mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
fnctl: add heroku-like app config interface (#277)
This commit is contained in:
committed by
Seif Lotfy سيف لطفي
parent
0aa76d6650
commit
d342aa1164
140
fnctl/apps.go
140
fnctl/apps.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -35,9 +36,33 @@ func apps() cli.Command {
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "describe",
|
||||
Usage: "describe an existing app",
|
||||
Action: a.describe,
|
||||
Name: "config",
|
||||
Usage: "operate an application configuration set",
|
||||
Action: a.configList,
|
||||
Flags: []cli.Flag{
|
||||
cli.BoolFlag{
|
||||
Name: "shell",
|
||||
Usage: "output in shell format",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "json",
|
||||
Usage: "output in JSON format",
|
||||
},
|
||||
},
|
||||
Subcommands: []cli.Command{
|
||||
{
|
||||
Name: "set",
|
||||
Description: "store a configuration key for this application",
|
||||
Usage: "<app> <key> <value>",
|
||||
Action: a.configSet,
|
||||
},
|
||||
{
|
||||
Name: "unset",
|
||||
Description: "remove a configuration key for this application",
|
||||
Usage: "<app> <key> <value>",
|
||||
Action: a.configUnset,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -87,7 +112,7 @@ func (a *appsCmd) create(c *cli.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *appsCmd) describe(c *cli.Context) error {
|
||||
func (a *appsCmd) configList(c *cli.Context) error {
|
||||
if c.Args().First() == "" {
|
||||
return errors.New("error: app description takes one argument, an app name")
|
||||
}
|
||||
@@ -102,15 +127,110 @@ func (a *appsCmd) describe(c *cli.Context) error {
|
||||
return fmt.Errorf("error creating app: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println("app:", wrapper.App.Name)
|
||||
if config := wrapper.App.Config; len(config) > 0 {
|
||||
fmt.Println("configuration:")
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', 0)
|
||||
fmt.Fprintln(w, "key\tvalue")
|
||||
config := wrapper.App.Config
|
||||
if len(config) == 0 {
|
||||
return errors.New("this application has no configurations")
|
||||
}
|
||||
|
||||
if c.Bool("json") {
|
||||
if err := json.NewEncoder(os.Stdout).Encode(config); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else if c.Bool("shell") {
|
||||
for k, v := range wrapper.App.Config {
|
||||
fmt.Fprint(w, k, "\t", v, "\n")
|
||||
fmt.Print("export ", k, "=", v, "\n")
|
||||
}
|
||||
} else {
|
||||
fmt.Println(wrapper.App.Name, "configuration:")
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', 0)
|
||||
for k, v := range config {
|
||||
fmt.Fprint(w, k, ":\t", v, "\n")
|
||||
}
|
||||
w.Flush()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *appsCmd) configSet(c *cli.Context) error {
|
||||
if c.Args().Get(0) == "" || c.Args().Get(1) == "" || c.Args().Get(2) == "" {
|
||||
return errors.New("error: application configuration setting takes three arguments: an app name, a key and a value")
|
||||
}
|
||||
|
||||
if err := resetBasePath(&a.Configuration); err != nil {
|
||||
return fmt.Errorf("error setting endpoint: %v", err)
|
||||
}
|
||||
|
||||
appName := c.Args().Get(0)
|
||||
key := c.Args().Get(1)
|
||||
value := c.Args().Get(2)
|
||||
|
||||
wrapper, _, err := a.AppsAppGet(appName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating app: %v", err)
|
||||
}
|
||||
|
||||
config := wrapper.App.Config
|
||||
|
||||
if config == nil {
|
||||
config = make(map[string]string)
|
||||
}
|
||||
|
||||
config[key] = value
|
||||
|
||||
if err := a.storeApp(appName, config); err != nil {
|
||||
return fmt.Errorf("error updating app configuration: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println(wrapper.App.Name, "updated", key, "with", value)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *appsCmd) configUnset(c *cli.Context) error {
|
||||
if c.Args().Get(0) == "" || c.Args().Get(1) == "" {
|
||||
return errors.New("error: application configuration setting takes three arguments: an app name, a key and a value")
|
||||
}
|
||||
|
||||
if err := resetBasePath(&a.Configuration); err != nil {
|
||||
return fmt.Errorf("error setting endpoint: %v", err)
|
||||
}
|
||||
|
||||
appName := c.Args().Get(0)
|
||||
key := c.Args().Get(1)
|
||||
|
||||
wrapper, _, err := a.AppsAppGet(appName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating app: %v", err)
|
||||
}
|
||||
|
||||
config := wrapper.App.Config
|
||||
|
||||
if config == nil {
|
||||
config = make(map[string]string)
|
||||
}
|
||||
|
||||
if _, ok := config[key]; !ok {
|
||||
return fmt.Errorf("configuration key %s not found", key)
|
||||
}
|
||||
|
||||
delete(config, key)
|
||||
|
||||
if err := a.storeApp(appName, config); err != nil {
|
||||
return fmt.Errorf("error updating app configuration: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println(wrapper.App.Name, "removed", key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *appsCmd) storeApp(appName string, config map[string]string) error {
|
||||
body := functions.AppWrapper{App: functions.App{
|
||||
Name: appName,
|
||||
Config: config,
|
||||
}}
|
||||
|
||||
if _, _, err := a.AppsPost(body); err != nil {
|
||||
return fmt.Errorf("error updating app configuration: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user