This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from ergomake/config-set-context
introduce config set-context command
- Loading branch information
Showing
11 changed files
with
360 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,14 +27,24 @@ jobs: | |
|
||
- run: go install . | ||
|
||
- name: Create Layerform config file | ||
- name: layerform config set-context | ||
run: | | ||
mkdir -p ~/.layerform | ||
echo "currentContext: test" > ~/.layerform/config | ||
echo "contexts:" >> ~/.layerform/config | ||
echo " test:" >> ~/.layerform/config | ||
echo " type: local" >> ~/.layerform/config | ||
echo " dir: test" >> ~/.layerform/config | ||
# validations, fails if command succeeds | ||
! layerform config set-context test -t local # missing --dir | ||
! layerform config set-context test -t s3 --bucket bucket # missing region | ||
! layerform config set-context test -t s3 --region region # missing bucket | ||
! layerform config set-context test -t cloud --url "invalid url" --email [email protected] --password strongpass | ||
! layerform config set-context test -t cloud --url https://a.b.com --email invalid --password strongpass | ||
! layerform config set-context test -t cloud --email invalid --password strongpass # missing url | ||
! layerform config set-context test -t cloud --url https://a.b.com --password strongpass # missing email | ||
! layerform config set-context test -t cloud --url https://a.b.com --email [email protected] # missing password | ||
# set valid contexts | ||
layerform config set-context test-s3 -t s3 --bucket bucket --region us-east-1 | ||
layerform config set-context test-cloud -t cloud --url https://demo.layerform.dev --email [email protected] --password strongpass | ||
layerform config set-context test-local -t local --dir test | ||
- name: Configure | ||
run: | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cli | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(configCmd) | ||
} | ||
|
||
var configCmd = &cobra.Command{ | ||
Use: "config", | ||
Short: "Modify layerform config file", | ||
Long: `Modify layerform config file using subcomands like "layerform config set-context"`, | ||
Example: `# Set a context entry in config | ||
layerform config set-context example --type=local --dir=~/.layerform/contexts/example`, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/ergomake/layerform/internal/lfconfig" | ||
) | ||
|
||
func init() { | ||
configSetContextCmd.Flags().StringP("type", "t", "local", "type of the context entry, must be \"local\", \"s3\" or \"cloud\"") | ||
configSetContextCmd.Flags().String("dir", "", "directory to store definitions and instances, required when type is \"local\"") | ||
configSetContextCmd.Flags().String("bucket", "", "bucket to store definitions and instances, required when type is \"s3\"") | ||
configSetContextCmd.Flags().String("region", "", "region where bucket is located, required when type is \"s3\"") | ||
configSetContextCmd.Flags().String("url", "", "url of layerform cloud, required when type is \"cloud\"") | ||
configSetContextCmd.Flags().String("email", "", "email of layerform cloud user, required when type is \"cloud\"") | ||
configSetContextCmd.Flags().String("password", "", "password of layerform cloud user, required when type is \"cloud\"") | ||
configSetContextCmd.Flags().SortFlags = false | ||
|
||
configCmd.AddCommand(configSetContextCmd) | ||
} | ||
|
||
var configSetContextCmd = &cobra.Command{ | ||
Use: "set-context <name>", | ||
Short: "Set a context entry in layerform config file", | ||
Long: `Set a context entry in layerform config file. | ||
Specifying a name that already exists will update that context values unless the type is different.`, | ||
Example: `# Set a context of type local named local-example | ||
layerform config set-context local-example -t local --dir example-dir | ||
# Set a context of type s3 named s3-example | ||
layerform config set-context s3-example -t s3 --bucket example-bucket --region us-east-1 | ||
# Set a context of type cloud named cloud-example | ||
layerform config set-context cloud-example -t cloud --url https://example.layerform.dev --email [email protected] --password secretpass`, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
name := args[0] | ||
|
||
t, _ := cmd.Flags().GetString("type") | ||
configCtx := lfconfig.ConfigContext{Type: t} | ||
switch configCtx.Type { | ||
case "local": | ||
dir, _ := cmd.Flags().GetString("dir") | ||
configCtx.Type = t | ||
configCtx.Dir = strings.TrimSpace(dir) | ||
case "s3": | ||
bucket, _ := cmd.Flags().GetString("bucket") | ||
region, _ := cmd.Flags().GetString("region") | ||
configCtx.Bucket = strings.TrimSpace(bucket) | ||
configCtx.Region = strings.TrimSpace(region) | ||
case "cloud": | ||
url, _ := cmd.Flags().GetString("url") | ||
email, _ := cmd.Flags().GetString("email") | ||
password, _ := cmd.Flags().GetString("password") | ||
configCtx.URL = strings.TrimSpace(url) | ||
configCtx.Email = strings.TrimSpace(email) | ||
configCtx.Password = strings.TrimSpace(password) | ||
default: | ||
fmt.Fprintf(os.Stderr, "invalid type %s\n", configCtx.Type) | ||
os.Exit(1) | ||
} | ||
|
||
err := lfconfig.Validate(configCtx) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "%s\n", errors.Wrap(err, "invalid context configuration")) | ||
os.Exit(1) | ||
} | ||
|
||
cfg, err := lfconfig.Load("") | ||
if err != nil && !errors.Is(err, os.ErrNotExist) { | ||
fmt.Fprintf(os.Stderr, "%s\n", errors.Wrap(err, "fail to open config file")) | ||
os.Exit(1) | ||
} | ||
|
||
action := "modified" | ||
if cfg == nil { | ||
action = "created" | ||
cfg, err = lfconfig.Init(name, configCtx, "") | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "%s\n", errors.Wrap(err, "fail to initialize empty config")) | ||
os.Exit(1) | ||
} | ||
} else { | ||
prev, ok := cfg.Contexts[name] | ||
if !ok { | ||
action = "created" | ||
} | ||
|
||
if ok && prev.Type != t { | ||
fmt.Fprintf( | ||
os.Stderr, | ||
"%s context already exists with a different type of %s, context type can't be updated.\n", | ||
name, | ||
cfg.GetCurrent().Type, | ||
) | ||
os.Exit(1) | ||
} | ||
cfg.Contexts[name] = configCtx | ||
} | ||
|
||
cfg.CurrentContext = name | ||
|
||
err = cfg.Save() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "%s\n", errors.Wrap(err, "fail to save config file")) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Fprintf(os.Stdout, "Context \"%s\" %s.\n", name, action) | ||
}, | ||
SilenceErrors: true, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.