Exemplo n.º 1
0
func main() {
	// If not set for some reason, use us-east-1 by default.
	if discfgDBRegion == "" {
		discfgDBRegion = "us-east-1"
	}

	apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) {
		var m message

		if err := json.Unmarshal(event, &m); err != nil {
			return nil, err
		}

		options.Storage.AWS.Region = discfgDBRegion
		// The following are set automatically.
		// options.Storage.AWS.AccessKeyID = os.Getenv("AWS_ACCESS_KEY_ID")
		// options.Storage.AWS.SecretAccessKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
		// options.Storage.AWS.SessionToken = os.Getenv("AWS_SESSION_TOKEN")

		// Each discfg API can be configured with a default table name.
		options.CfgName = discfgDBTable
		// Overwritten by the message passed to the Lambda.
		if m.Name != "" {
			options.CfgName = m.Name
		}
		// Comes from a path param from API Gateway.
		options.Key = m.Key
		// Comes from a querystring value from API Gateway, ie. ?ttl=300
		// Note: 0 is unlimited, no TTL.
		if m.TTL != "" {
			if ttl, err := strconv.ParseInt(m.TTL, 10, 64); err == nil {
				options.TTL = ttl
			}
		}
		// Ends up being the POST body from API Gateway.
		options.Value = []byte(m.Value)

		resp := commands.SetKey(options)

		// Format the expiration time (if applicable). This prevents output like "0001-01-01T00:00:00Z" when empty
		// and allows for the time.RFC3339Nano format to be used whereas time.Time normally marshals to a different format.
		if resp.Item.TTL > 0 {
			resp.Item.OutputExpiration = resp.Item.Expiration.Format(time.RFC3339Nano)
		}

		return commands.FormatJSONValue(resp), nil
	})
}
Exemplo n.º 2
0
	Short: "config information",
	Long:  `Information about the config including version and modified time`,
	Run: func(cmd *cobra.Command, args []string) {
		setOptsFromArgs(args)
		resp := commands.Info(Options)
		commands.Out(Options, resp)
	},
}

var setCmd = &cobra.Command{
	Use:   "set",
	Short: "set key value",
	Long:  `Sets a key value for a given discfg`,
	Run: func(cmd *cobra.Command, args []string) {
		setOptsFromArgs(args)
		resp := commands.SetKey(Options)
		commands.Out(Options, resp)
	},
}
var getCmd = &cobra.Command{
	Use:   "get",
	Short: "get key value",
	Long:  `Gets a key value for a given discfg`,
	Run: func(cmd *cobra.Command, args []string) {
		setOptsFromArgs(args)
		resp := commands.GetKey(Options)
		commands.Out(Options, resp)
	},
}
var deleteCmd = &cobra.Command{
	Use:   "delete",