From a9d15bb320a777d8ccfaca2d24a4e3b4cab5350f Mon Sep 17 00:00:00 2001 From: Victor Vrantchan Date: Sat, 18 Mar 2017 22:22:02 +0000 Subject: [PATCH] apply DEP profile --- apply.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 2 ++ 2 files changed, 75 insertions(+) create mode 100644 apply.go diff --git a/apply.go b/apply.go new file mode 100644 index 00000000..8054f2a7 --- /dev/null +++ b/apply.go @@ -0,0 +1,73 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "io/ioutil" + "os" + "strings" + + "github.com/micromdm/dep" + "github.com/pkg/errors" +) + +func applyResource(args []string) error { + if len(args) < 1 { + return errors.New("apply requires at least one resource name") + } + + sm := &config{} + sm.setupBolt() + client, err := sm.depClient() + if err != nil { + return err + } + + var run func(dep.Client, []string) error + switch strings.ToLower(args[0]) { + case "dep-profile": + run = defineDEPProfile + default: + usage() + os.Exit(1) + } + + if err := run(client, args[1:]); err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + os.Exit(1) + } + + return nil +} + +func defineDEPProfile(client dep.Client, args []string) error { + flagset := flag.NewFlagSet("dep-profile", flag.ExitOnError) + var ( + flPath = flagset.String("f", "", "path to dep profile JSON") + ) + if err := flagset.Parse(args); err != nil { + return err + } + + if *flPath == "" { + return errors.New("must specify a path to a profile json") + } + + data, err := ioutil.ReadFile(*flPath) + if err != nil { + return errors.Wrapf(err, "reading DEP profile JSON file: %s", *flPath) + } + + var profile dep.Profile + if err := json.Unmarshal(data, &profile); err != nil { + return err + } + + resp, err := client.DefineProfile(&profile) + if err != nil { + return err + } + fmt.Printf("updated profile %s.\n", resp.ProfileUUID) + return nil +} diff --git a/main.go b/main.go index dc38429a..78b4add0 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,8 @@ func main() { run = depToken case "get": run = getResource + case "apply": + run = applyResource default: usage() os.Exit(1)