// DeleteKey deletes a key from a configuration func DeleteKey(opts config.Options) config.ResponseObject { resp := config.ResponseObject{ Action: "delete", } key, keyErr := formatKeyName(opts.Key) if keyErr == nil { opts.Key = key storageResponse, err := storage.Delete(opts) if err != nil { resp.Error = err.Error() resp.Message = "Error getting key value" } else { resp.Item = storageResponse resp.Item.Key = opts.Key resp.Item.Value = nil resp.Item.Version = storageResponse.Version + 1 resp.PrevItem.Key = opts.Key resp.PrevItem.Version = storageResponse.Version resp.PrevItem.Value = storageResponse.Value // log.Println(storageResponse) } } else { resp.Error = NotEnoughArgsMsg } return resp }
// Info about the configuration including global version/state and modified time func Info(opts config.Options) config.ResponseObject { resp := config.ResponseObject{ Action: "info", } if opts.CfgName != "" { // Just get the root key opts.Key = "/" storageResponse, err := storage.Get(opts) if err != nil { resp.Error = err.Error() } else { // Debating putting the item value on here... (allowing users to store values on the config or "root") // resp.Item = storageResponse // Set the configuration version and modified time on the response // Item.CfgVersion and Item.CfgModifiedNanoseconds are not included in the JSON output resp.CfgVersion = storageResponse.CfgVersion resp.CfgModified = 0 resp.CfgModifiedNanoseconds = storageResponse.CfgModifiedNanoseconds // Modified in seconds resp.CfgModified = storageResponse.CfgModifiedNanoseconds / int64(time.Second) // Modified parsed modified := time.Unix(0, storageResponse.CfgModifiedNanoseconds) resp.CfgModifiedParsed = modified.Format(time.RFC3339) // Set information about the storage engine resp.CfgStorage.InterfaceName = opts.StorageInterfaceName resp.CfgStorage.Name = storage.Name(opts) resp.CfgStorage.Options = storage.Options(opts) // Get the status (only applicable for some storage interfaces, such as DynamoDB) resp.CfgState, err = storage.ConfigState(opts) if err != nil { resp.Error = err.Error() } else { var buffer bytes.Buffer buffer.WriteString(opts.CfgName) if resp.CfgState != "" { buffer.WriteString(" (") buffer.WriteString(resp.CfgState) buffer.WriteString(")") } buffer.WriteString(" version ") buffer.WriteString(strconv.FormatInt(resp.CfgVersion, 10)) buffer.WriteString(" last modified ") buffer.WriteString(modified.Format(time.RFC1123)) resp.Message = buffer.String() buffer.Reset() } } } else { resp.Error = NotEnoughArgsMsg } return resp }
// GetKey gets a key from a configuration func GetKey(opts config.Options) config.ResponseObject { resp := config.ResponseObject{ Action: "get", } key, keyErr := formatKeyName(opts.Key) if keyErr == nil { opts.Key = key storageResponse, err := storage.Get(opts) if err != nil { resp.Error = err.Error() } else { resp.Item = storageResponse } } else { resp.Error = keyErr.Error() } return resp }
// SetKey sets a key value for a given configuration func SetKey(opts config.Options) config.ResponseObject { resp := config.ResponseObject{ Action: "set", } // Do not allow empty values to be set if opts.Value == nil { resp.Error = ValueRequiredMsg return resp } if opts.CfgName == "" { resp.Error = MissingCfgNameMsg return resp } key, keyErr := formatKeyName(opts.Key) if keyErr == nil { opts.Key = key storageResponse, err := storage.Update(opts) if err != nil { resp.Error = err.Error() resp.Message = "Error updating key value" } else { resp.Item.Key = key resp.Item.Value = opts.Value resp.Item.Version = 1 // Only set PrevItem if there was a previous value if storageResponse.Value != nil { resp.PrevItem = storageResponse resp.PrevItem.Key = key // Update the current item's value if there was a previous version resp.Item.Version = resp.PrevItem.Version + 1 } } } else { resp.Error = keyErr.Error() } return resp }
"encoding/json" "fmt" "github.com/spf13/cobra" "github.com/tmaiaroto/discfg/commands" "github.com/tmaiaroto/discfg/config" "github.com/tmaiaroto/discfg/version" "io/ioutil" "os" "time" ) var _ time.Duration var _ bytes.Buffer // Options for the configuration var Options = config.Options{StorageInterfaceName: "dynamodb", Version: version.Semantic} // dataFile for loading data for a key from file using the CLI var dataFile = "" // DiscfgCmd defines the parent discfg command var DiscfgCmd = &cobra.Command{ Use: "discfg", Short: "discfg is a distributed configuration service", Long: `A distributed configuration service using Amazon Web Services.`, Run: func(cmd *cobra.Command, args []string) {}, } // versionCmd displays the discfg version var versionCmd = &cobra.Command{ Use: "version",