diff --git a/.github/workflows/integration-info.yml b/.github/workflows/integration-info.yml new file mode 100644 index 00000000..932b6810 --- /dev/null +++ b/.github/workflows/integration-info.yml @@ -0,0 +1,26 @@ +name: Draft Info Integration Test +on: + pull_request: + branches: [ main ] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.18.2 + - name: make + run: make + - run: | + ./draft info > ./info.json + echo "Draft Info JSON schema:" + cat test/info_schema.json + echo "Draft Info JSON:" + cat info.json + - name: Validate JSON + run: | + npm install -g ajv-cli + ajv validate -s test/info_schema.json -d info.json \ No newline at end of file diff --git a/README.md b/README.md index 45cfaa47..5097217e 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,33 @@ If you don’t plan on using the GitHub Action, you can directly apply your depl If you plan on deploying your application through your GitHub Action, commit all the files to your repository and watch your application get deployed! +### `draft info` +The `draft info` command prints information about supported languages and deployment types. +Example output: +``` +{ + "supported_languages": [ + "php", + "python", + "rust", + "swift", + "csharp", + "go", + "gradle", + "javascript", + "ruby", + "clojure", + "erlang", + "gomodule", + "java" + ], + "supported_deployment_types": [ + "helm", + "kustomize", + "manifests" + ] +} +``` ## About The Project @@ -61,6 +88,7 @@ Draft makes it easier for developers to get started building apps that run on Ku - `draft setup-gh` automates the GitHub OIDC setup process for your project. - `draft generate-workflow` generates a GitHub Actions workflow for automatic build and deploy to a Kubernetes cluster. - `draft update` automatically make your application to be internet accessible. +- `draft info` print supported language and field information in json format. Use `draft [command] --help` for more information about a command. diff --git a/cmd/info.go b/cmd/info.go new file mode 100644 index 00000000..46734267 --- /dev/null +++ b/cmd/info.go @@ -0,0 +1,70 @@ +package cmd + +import ( + "encoding/json" + "fmt" + + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + + "github.com/Azure/draft/pkg/deployments" + "github.com/Azure/draft/pkg/languages" + "github.com/Azure/draft/template" +) + +type Format string + +const ( + JSON Format = "json" +) + +type infoCmd struct { + format string + info *draftInfo +} + +type draftInfo struct { + SupportedLanguages []string `json:"supported_languages"` + SupportedDeploymentTypes []string `json:"supported_deployment_types"` +} + +func newInfoCmd() *cobra.Command { + ic := &infoCmd{} + var cmd = &cobra.Command{ + Use: "info", + Short: "Prints draft supported values in machine-readable format", + Long: `This command prints information about the current draft environment and supported values such as supported dockerfile languages and deployment manifest types.`, + RunE: func(cmd *cobra.Command, args []string) error { + if err := ic.run(); err != nil { + return err + } + return nil + }, + } + f := cmd.Flags() + f.StringVarP(&ic.format, "format", "f", ".", "specify the format to print draft information in (json, yaml, etc)") + + return cmd +} + +func (ic *infoCmd) run() error { + log.Debugf("getting supported languages") + l := languages.CreateLanguagesFromEmbedFS(template.Dockerfiles, "") + d := deployments.CreateDeploymentsFromEmbedFS(template.Deployments, "") + + ic.info = &draftInfo{ + SupportedLanguages: l.Names(), + SupportedDeploymentTypes: d.DeployTypes(), + } + + infoText, err := json.MarshalIndent(ic.info, "", " ") + if err != nil { + return fmt.Errorf("could not marshal draft info into json: %w", err) + } + fmt.Println(string(infoText)) + return nil +} + +func init() { + rootCmd.AddCommand(newInfoCmd()) +} diff --git a/pkg/deployments/deployments.go b/pkg/deployments/deployments.go index 13a492a9..54ccf029 100644 --- a/pkg/deployments/deployments.go +++ b/pkg/deployments/deployments.go @@ -6,6 +6,7 @@ import ( "io/fs" log "github.com/sirupsen/logrus" + "golang.org/x/exp/maps" "gopkg.in/yaml.v3" "github.com/Azure/draft/pkg/config" @@ -26,6 +27,12 @@ type Deployments struct { deploymentTemplates fs.FS } +// DeployTypes returns a slice of the supported deployment types +func (d *Deployments) DeployTypes() []string { + names := maps.Keys(d.deploys) + return names +} + func (d *Deployments) CopyDeploymentFiles(deployType string, customInputs map[string]string, templateWriter templatewriter.TemplateWriter) error { val, ok := d.deploys[deployType] if !ok { diff --git a/pkg/languages/languages.go b/pkg/languages/languages.go index bd8a3e8b..bfcf1093 100644 --- a/pkg/languages/languages.go +++ b/pkg/languages/languages.go @@ -6,6 +6,7 @@ import ( "io/fs" log "github.com/sirupsen/logrus" + "golang.org/x/exp/maps" "gopkg.in/yaml.v3" "github.com/Azure/draft/pkg/config" @@ -25,6 +26,12 @@ type Languages struct { dockerfileTemplates fs.FS } +// Names returns a slice of the names of the supported languages +func (l *Languages) Names() []string { + names := maps.Keys(l.langs) + return names +} + func (l *Languages) ContainsLanguage(lang string) bool { _, ok := l.langs[lang] return ok diff --git a/test/info_schema.json b/test/info_schema.json new file mode 100644 index 00000000..73fdada0 --- /dev/null +++ b/test/info_schema.json @@ -0,0 +1,38 @@ +{ + "definitions": {}, + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Root", + "type": "object", + "required": [ + "supported_languages", + "supported_deployment_types" + ], + "properties": { + "supported_languages": { + "$id": "#root/supported_languages", + "title": "Supported_languages", + "type": "array", + "default": [], + "items":{ + "$id": "#root/supported_languages/items", + "title": "Items", + "type": "string", + "default": "", + "pattern": "^.*$" + } + }, + "supported_deployment_types": { + "$id": "#root/supported_deployment_types", + "title": "Supported_deployment_types", + "type": "array", + "default": [], + "items":{ + "$id": "#root/supported_deployment_types/items", + "title": "Items", + "type": "string", + "default": "", + "pattern": "^.*$" + } + } + } +}