Skip to content

Commit

Permalink
fix: help docs and some clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sunggun-yu committed Feb 7, 2022
1 parent 2f0b390 commit dd81820
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 99 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ brew install sunggun-yu/tap/envp
ENVP is cli wrapper that sets environment variables by profile based configuration when you execute the command line
Usage:
envp profile-name [flags] -- [command line to execute, such like kubectl]
envp profile-name [flags] -- [command line to execute, e.g. kubectl]
envp [command]
Examples:
Expand All @@ -87,13 +87,14 @@ Examples:
Available Commands:
add add profile
add Add environment variable profile
completion Generate the autocompletion script for the specified shell
delete
edit edit profile
delete Delete environment variable profile
edit Edit environment variable profile
help Help about any command
list List all profiles
use Set default profile
list List all environment variable profiles
show Print the environment variables of profile
use Set default environment variable profile
version Print the version of envp
Flags:
Expand Down
19 changes: 9 additions & 10 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,23 @@ func init() {
func cmdExampleAdd() string {
return `
envp add my-proxy \
-d "profile desc" \
-d 'profile desc' \
-e 'VAR=VAL' \
-e HTTPS_PROXY=http://some-proxy:3128 \
-e "NO_PROXY=127.0.0.1,localhost" \
-e "DOCKER_HOST=ssh://myuser@some-server"
-e 'NO_PROXY=127.0.0.1,localhost' \
-e 'DOCKER_HOST=ssh://myuser@some-server'
`
}

// add command
// addCommand add/create environment variable profile and it's envionment variables in the config file
func addCommand() *cobra.Command {
// add flags
var flags addFlags

// add command
cmd := &cobra.Command{
Use: "add [profile-name-with-no-space] [flags]",
Short: "add profile",
Use: "add profile-name",
Short: "Add environment variable profile",
SilenceUsage: true,
Example: cmdExampleAdd(),
Args: cobra.MatchAll(
Expand Down Expand Up @@ -87,10 +88,8 @@ func addCommand() *cobra.Command {
},
}

// set optional flag "profile". so that user can select profile without swithing it
// selected profile by `use` command should be the profile if it is omitted
cmd.Flags().StringVarP(&flags.desc, "desc", "d", "", "description of profile")
cmd.Flags().StringSliceVarP(&flags.env, "env", "e", []string{}, "usage string")

cmd.Flags().StringSliceVarP(&flags.env, "env", "e", []string{}, "'VAR=VAL' format of string")
cmd.MarkFlagRequired("env")
return cmd
}
98 changes: 53 additions & 45 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,67 @@ import (
)

func init() {
rootCmd.AddCommand(deleteCmd)
rootCmd.AddCommand(deleteCommand())
}

var deleteCmd = &cobra.Command{
Use: "delete profile-name",
Long: "Delete profile from config. please set default profile in case you delete default profile",
Aliases: []string{"del"},
SilenceUsage: true,
Example: `
# delete profile
// example of delete command
func cmdExampleDelete() string {
return `
envp delete profile
envp del another-profile
`,
Args: cobra.MatchAll(
Arg0AsProfileName(),
Arg0NotExistingProfile(),
),
RunE: func(cmd *cobra.Command, args []string) error {
p := args[0]
`
}

// use built-in function to delete key(profile) from map (profiles)
delete(viper.Get(ConfigKeyProfile).(map[string]interface{}), p)
// deleteCommand delete/remove environment variable profile and it's envionment variables from the config file
func deleteCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete profile-name",
Short: "Delete environment variable profile",
Aliases: []string{"del"},
SilenceUsage: true,
Example: cmdExampleDelete(),
Args: cobra.MatchAll(
Arg0AsProfileName(),
Arg0NotExistingProfile(),
),
RunE: func(cmd *cobra.Command, args []string) error {
p := args[0]

// set default="" if default profile is being deleted
if p == viper.GetString(ConfigKeyDefaultProfile) {
viper.Set(ConfigKeyDefaultProfile, "")
fmt.Println("WARN: Deleting default profile. please set default profile once it is deleted")
}
// use built-in function to delete key(profile) from map (profiles)
delete(viper.Get(ConfigKeyProfile).(map[string]interface{}), p)

// TODO: study viper more. watch may not needed if viper.WriteConfig() reloads config after writing file.
// watch config changes
viper.WatchConfig()
// wait for the config file update and verify profile is added or not
rc := make(chan error, 1)
// I think underlying of viper.OnConfiChange is goroutine. but just run it as goroutine just in case
go viper.OnConfigChange(func(e fsnotify.Event) {
if viper.Sub(ConfigKeyProfile).Get(p) != nil {
rc <- fmt.Errorf("profile %v not deleted", p)
return
// set default="" if default profile is being deleted
if p == viper.GetString(ConfigKeyDefaultProfile) {
viper.Set(ConfigKeyDefaultProfile, "")
fmt.Println("WARN: Deleting default profile. please set default profile once it is deleted")
}
fmt.Println("Profile", p, "deleted successfully:", e.Name)
rc <- nil
})

if err := viper.WriteConfig(); err != nil {
return err
}
// TODO: study viper more. watch may not needed if viper.WriteConfig() reloads config after writing file.
// watch config changes
viper.WatchConfig()
// wait for the config file update and verify profile is added or not
rc := make(chan error, 1)
// I think underlying of viper.OnConfiChange is goroutine. but just run it as goroutine just in case
go viper.OnConfigChange(func(e fsnotify.Event) {
if viper.Sub(ConfigKeyProfile).Get(p) != nil {
rc <- fmt.Errorf("profile %v not deleted", p)
return
}
fmt.Println("Profile", p, "deleted successfully:", e.Name)
rc <- nil
})

if err := viper.WriteConfig(); err != nil {
return err
}

// wait for profile validation channel
err := <-rc
if err != nil {
return err
}
return nil
},
// wait for profile validation channel
err := <-rc
if err != nil {
return err
}
return nil
},
}
return cmd
}
15 changes: 8 additions & 7 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/sunggun-yu/envp/internal/config"
)

// flags struct for edit command
type editFlags struct {
desc string
env []string
Expand All @@ -22,17 +23,18 @@ func init() {
func cmdExampleEdit() string {
return `
envp edit my-proxy \
-d "updated profile desc" \
-e "NO_PROXY=127.0.0.1,localhost"
-d 'updated profile desc' \
-e 'NO_PROXY=127.0.0.1,localhost'
`
}

// editCommand edit/update environment variable profile and it's envionment variables in the config file
func editCommand() *cobra.Command {
var flags editFlags

cmd := &cobra.Command{
Use: "edit [profile-name-with-no-space] [flags]",
Short: "edit profile",
Use: "edit profile-name [flags]",
Short: "Edit environment variable profile",
SilenceUsage: true,
Example: cmdExampleEdit(),
Args: cobra.MatchAll(
Expand Down Expand Up @@ -105,10 +107,9 @@ func editCommand() *cobra.Command {
},
}

// set optional flag "profile". so that user can select profile without swithing profile
// selected profile by `use` command should be the profile if it is omitted
cmd.Flags().StringVarP(&flags.desc, "desc", "d", "", "description of profile")
cmd.Flags().StringSliceVarP(&flags.env, "env", "e", []string{}, "usage string")
cmd.Flags().StringSliceVarP(&flags.env, "env", "e", []string{}, "'VAR=VAL' format of string")
cmd.MarkFlagRequired("env")

return cmd
}
3 changes: 2 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ func cmdExampleList() string {
`
}

// listCommand prints out list of environment variable profiles
func listCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List all profile names",
Short: "List all environment variable profiles",
Aliases: []string{"ls"},
SilenceUsage: true,
Example: cmdExampleList(),
Expand Down
17 changes: 10 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ const (

var rootCmd = rootCommand()

// Execute execute the root command and sub commands
func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}

func init() {
cobra.OnInitialize(initConfig)
}
Expand All @@ -36,6 +43,7 @@ func cmdExampleRoot() string {
`
}

// rootCommand sets environment variable and execute command line
func rootCommand() *cobra.Command {
// profile name from flag or config section "use"
var profile string
Expand All @@ -45,7 +53,7 @@ func rootCommand() *cobra.Command {
var currentProfile config.Profile

cmd := &cobra.Command{
Use: "envp profile-name [flags] -- [command line to execute, such like kubectl]",
Use: "envp profile-name [flags] -- [command line to execute, e.g. kubectl]",
Short: "ENVP is cli wrapper that sets environment variables by profile based configuration when you execute the command line",
SilenceUsage: true,
Example: cmdExampleRoot(),
Expand Down Expand Up @@ -120,6 +128,7 @@ func rootCommand() *cobra.Command {
return cmd
}

// initConfig read the config file and initialize if config file is not existing
func initConfig() {
// set default empty profile name
viper.SetDefault("default", "")
Expand All @@ -141,12 +150,6 @@ func initConfig() {
viper.WriteConfig()
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}

// get config path. mkdir -p it not exist
func configPath(base string) string {
// get $HOME
Expand Down
6 changes: 4 additions & 2 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/sunggun-yu/envp/internal/config"
)

// flags struct for show command
type showFlags struct {
export bool
}
Expand All @@ -16,7 +17,7 @@ func init() {
rootCmd.AddCommand(showCommand())
}

// example of edit command
// example of show command
func cmdExampleShow() string {
return `
envp show
Expand All @@ -26,13 +27,14 @@ func cmdExampleShow() string {
`
}

// showCommand prints out all the environment variables of profile
func showCommand() *cobra.Command {
var flags showFlags
var profileName string

cmd := &cobra.Command{
Use: "show profile-name [flags]",
Short: "show profile-name",
Short: "Print the environment variables of profile",
SilenceUsage: true,
Example: cmdExampleShow(),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
49 changes: 29 additions & 20 deletions cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,40 @@ import (
)

func init() {
rootCmd.AddCommand(useCmd)
rootCmd.AddCommand(useCommand())
}

var useCmd = &cobra.Command{
Use: "use profile-name",
Short: "Set default profile",
SilenceUsage: true,
Example: `
// example of use command
func cmdExampleUse() string {
return `
# set default profile to profile-name
envp use profile-name
# env vars in the default profile will be set during command execution
envp -- kubectl get pods
`,
Args: cobra.MatchAll(
Arg0AsProfileName(),
Arg0NotExistingProfile(),
),
RunE: func(cmd *cobra.Command, args []string) error {
p := args[0]
viper.Set(ConfigKeyDefaultProfile, p)
if err := viper.WriteConfig(); err != nil {
return fmt.Errorf("failed to updating the config file: %v", err.Error())
}
fmt.Println("Default profile is set to", viper.GetString(ConfigKeyDefaultProfile))
return nil
},
`
}

// add command
func useCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "use profile-name",
Short: "Set default environment variable profile",
SilenceUsage: true,
Example: cmdExampleUse(),
Args: cobra.MatchAll(
Arg0AsProfileName(),
Arg0NotExistingProfile(),
),
RunE: func(cmd *cobra.Command, args []string) error {
p := args[0]
viper.Set(ConfigKeyDefaultProfile, p)
if err := viper.WriteConfig(); err != nil {
return fmt.Errorf("failed to updating the config file: %v", err.Error())
}
fmt.Println("Default profile is set to", viper.GetString(ConfigKeyDefaultProfile))
return nil
},
}
return cmd
}
1 change: 1 addition & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/spf13/cobra"
)

// make it public to set version from main.go
var Version string

func init() {
Expand Down
Loading

0 comments on commit dd81820

Please sign in to comment.