-
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
89ebbe7
commit 88936f9
Showing
3 changed files
with
129 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,78 @@ | ||
/* | ||
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) { | ||
configDir, _ := os.Getwd() | ||
|
||
configured := false | ||
if len(args) > 0 { | ||
for _, pair := range config.ParseConfig(args) { | ||
err := config.WriteLocalConfigValue(configDir, pair.Key, pair.Value) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
configured = true | ||
} | ||
|
||
if !configured { | ||
printLocalConfigHelp(cmd) | ||
} else { | ||
logger.Infof("set CLI config for: %s", configDir) | ||
} | ||
}, | ||
} | ||
|
||
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 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
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