Skip to content

Commit

Permalink
Added PSTOREPATH_
Browse files Browse the repository at this point in the history
  • Loading branch information
aidansteele committed Feb 22, 2018
1 parent 4aa35c0 commit 4764d9d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
7 changes: 4 additions & 3 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
simplePrefix := viper.GetString("prefix")
tagPrefix := viper.GetString("tag-prefix")
pathPrefix := viper.GetString("path-prefix")
verbose := viper.GetBool("verbose")

doExec(simplePrefix, tagPrefix, verbose, args)
doExec(simplePrefix, tagPrefix, pathPrefix, verbose, args)
},
}

func doExec(simplePrefix, tagPrefix string, verbose bool, args []string) {
common.Doit(simplePrefix, tagPrefix, verbose, func(key, val string) {
func doExec(simplePrefix, tagPrefix, pathPrefix string, verbose bool, args []string) {
common.Doit(simplePrefix, tagPrefix, pathPrefix, verbose, func(key, val string) {
os.Setenv(key, val)
})

Expand Down
7 changes: 4 additions & 3 deletions cmd/powershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
simplePrefix := viper.GetString("prefix")
tagPrefix := viper.GetString("tag-prefix")
pathPrefix := viper.GetString("path-prefix")
verbose := viper.GetBool("verbose")

doPowershell(simplePrefix, tagPrefix, verbose)
doPowershell(simplePrefix, tagPrefix, pathPrefix, verbose)
},
}

func doPowershell(simplePrefix, tagPrefix string, verbose bool) {
common.Doit(simplePrefix, tagPrefix, verbose, func(key, val string) {
func doPowershell(simplePrefix, tagPrefix, pathPrefix string, verbose bool) {
common.Doit(simplePrefix, tagPrefix, pathPrefix, verbose, func(key, val string) {
escaped := strings.Replace(val, "\"", "\\\"", -1)
fmt.Printf("${Env:%s}=\"%s\"\n", key, escaped)
})
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func init() {

RootCmd.PersistentFlags().String("prefix", "PSTORE_", "")
RootCmd.PersistentFlags().String("tag-prefix", "PSTORETAG_", "")
RootCmd.PersistentFlags().String("path-prefix", "PSTOREPATH_", "")
RootCmd.PersistentFlags().Bool("verbose", false, "")

RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.pstore.yaml)")
Expand Down
7 changes: 4 additions & 3 deletions cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
simplePrefix := viper.GetString("prefix")
tagPrefix := viper.GetString("tag-prefix")
pathPrefix := viper.GetString("path-prefix")
verbose := viper.GetBool("verbose")

doShell(simplePrefix, tagPrefix, verbose)
doShell(simplePrefix, tagPrefix, pathPrefix, verbose)
},
}

func doShell(simplePrefix, tagPrefix string, verbose bool) {
common.Doit(simplePrefix, tagPrefix, verbose, func(key, val string) {
func doShell(simplePrefix, tagPrefix, pathPrefix string, verbose bool) {
common.Doit(simplePrefix, tagPrefix, pathPrefix, verbose, func(key, val string) {
escaped := strings.Replace(val, "'", "\\'", -1)
fmt.Printf("export %s='%s'\n", key, escaped)
})
Expand Down
49 changes: 45 additions & 4 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,49 @@ func GetParamsByNames(sess *session.Session, input map[string]string) []ParamRes
return results
}

func GetParamsByPaths(sess *session.Session, input []string) []ParamResult {
results := []ParamResult{}
api := ssm.New(sess)
requestId := ""

for _, path := range input {
resp, err := api.GetParametersByPathWithContext(context.Background(), &ssm.GetParametersByPathInput{
Path: &path,
Recursive: aws.Bool(false),
}, func(r *request.Request) {
r.Handlers.Complete.PushBack(func(req *request.Request) {
requestId = req.RequestID
})
})


for _, param := range resp.Parameters {
parts := strings.Split(*param.Name, "/")
name := parts[len(parts)-1]
if err == nil {
results = append(results, ParamResult{
ParamName: "",
EnvName: name,
Value: *param.Value,
RequestId: requestId,
Success: true,
})
}
}

}

return results
}


type ParamsRequest struct {
SimpleParams map[string]string
TaggedParams map[string]string
PathParams []string
}

func GetParamRequestFromEnv(simplePrefix, tagPrefix string) ParamsRequest {
func GetParamRequestFromEnv(simplePrefix, tagPrefix, pathPrefix string) ParamsRequest {
req := ParamsRequest{
SimpleParams: make(map[string]string),
TaggedParams: make(map[string]string),
Expand All @@ -160,6 +197,8 @@ func GetParamRequestFromEnv(simplePrefix, tagPrefix string) ParamsRequest {
} else if strings.HasPrefix(name, tagPrefix) {
shortName := name[len(tagPrefix):]
req.TaggedParams[shortName] = value
} else if strings.HasPrefix(name, pathPrefix) {
req.PathParams = append(req.PathParams, value)
}
}

Expand Down Expand Up @@ -199,9 +238,9 @@ func awsRegion() string {
return region
}

func Doit(simplePrefix, tagPrefix string, verbose bool, callback func(key, value string)) {
req := GetParamRequestFromEnv(simplePrefix, tagPrefix)
if len(req.TaggedParams) + len(req.SimpleParams) == 0 { return }
func Doit(simplePrefix, tagPrefix, pathPrefix string, verbose bool, callback func(key, value string)) {
req := GetParamRequestFromEnv(simplePrefix, tagPrefix, pathPrefix)
if len(req.TaggedParams) + len(req.SimpleParams) + len(req.PathParams) == 0 { return }

region := awsRegion()
if len(region) == 0 {
Expand All @@ -213,6 +252,8 @@ func Doit(simplePrefix, tagPrefix string, verbose bool, callback func(key, value

results := GetParamsByNames(sess, req.SimpleParams)

results = append(results, GetParamsByPaths(sess, req.PathParams)...)

for key, val := range req.TaggedParams {
results = append(results, GetParametersByTag(sess, key, val)...)
}
Expand Down

0 comments on commit 4764d9d

Please sign in to comment.