-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds local config writer command.
- Loading branch information
1 parent
457f7dc
commit b1a247d
Showing
4 changed files
with
189 additions
and
11 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
Copyright © 2021-2023 Pete Cornish <[email protected]> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"gatehill.io/imposter/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// localConfigCmd represents the localConfig command | ||
var localConfigCmd = &cobra.Command{ | ||
Use: "config [key=value]", | ||
Short: "Set CLI config for working directory", | ||
Long: `Sets CLI configuration for the working directory.`, | ||
Args: cobra.MinimumNArgs(0), | ||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
var formattedKeys []string | ||
for _, k := range listSupportedLocalKeys() { | ||
formattedKeys = append(formattedKeys, k+"=VAL") | ||
} | ||
return formattedKeys, cobra.ShellCompDirectiveNoFileComp | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) > 0 { | ||
configDir, _ := os.Getwd() | ||
setLocalConfig(cmd, args, configDir) | ||
} else { | ||
printLocalConfigHelp(cmd) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(localConfigCmd) | ||
} | ||
|
||
func printLocalConfigHelp(cmd *cobra.Command) { | ||
supported := strings.Join(listSupportedLocalKeys(), ", ") | ||
fmt.Fprintf(os.Stderr, "%v\nSupported config keys: %s\n", cmd.UsageString(), supported) | ||
os.Exit(1) | ||
} | ||
|
||
func setLocalConfig(cmd *cobra.Command, args []string, configDir string) { | ||
parsed := config.ParseConfig(args) | ||
if len(parsed) == 0 { | ||
printLocalConfigHelp(cmd) | ||
} else { | ||
for _, pair := range parsed { | ||
err := config.WriteLocalConfigValue(configDir, pair.Key, pair.Value) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
logger.Infof("set CLI config for: %s", configDir) | ||
} | ||
} | ||
|
||
func listSupportedLocalKeys() []string { | ||
return []string{ | ||
"engine", | ||
"version", | ||
} | ||
} |
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,57 @@ | ||
package cmd | ||
|
||
import ( | ||
"gatehill.io/imposter/config" | ||
"github.com/spf13/cobra" | ||
"os" | ||
"path" | ||
"testing" | ||
) | ||
|
||
func Test_setLocalConfig(t *testing.T) { | ||
type args struct { | ||
cmd *cobra.Command | ||
args []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
fileShouldExist bool | ||
want string | ||
}{ | ||
{ | ||
name: "set local config", | ||
args: args{ | ||
cmd: &cobra.Command{}, | ||
args: []string{"foo=bar"}, | ||
}, | ||
wantErr: false, | ||
fileShouldExist: true, | ||
want: "foo: bar\n", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tempDir, err := os.MkdirTemp(os.TempDir(), "imposter-cli") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
setLocalConfig(tt.args.cmd, tt.args.args, tempDir) | ||
cfg, err := os.ReadFile(path.Join(tempDir, config.LocalDirConfigFileName+".yaml")) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("setLocalConfig() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if tt.fileShouldExist && err != nil { | ||
t.Errorf("setLocalConfig() file should exist but does not") | ||
return | ||
} | ||
if tt.want != string(cfg) { | ||
t.Errorf("file contents do not match expected - got %s, want %s", string(cfg), tt.want) | ||
return | ||
} | ||
}) | ||
} | ||
} |
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
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