diff --git a/cmd/mdmctl/apply_dep_profile.go b/cmd/mdmctl/apply_dep_profile.go index 574504c5..34352a27 100644 --- a/cmd/mdmctl/apply_dep_profile.go +++ b/cmd/mdmctl/apply_dep_profile.go @@ -66,17 +66,25 @@ func (cmd *applyCommand) applyDEPProfile(args []string) error { if *flProfilePath == "" { flagset.Usage() - return errors.New("bad input: must provide -f parameter") + return errors.New("bad input: must provide -f or -template parameter ") } - pf, err := os.Open(*flProfilePath) - if err != nil { - return errors.Wrap(err, "opening DEP profile file") + var output *os.File + { + if *flProfilePath == "-" { + output = os.Stdin + } else { + var err error + output, err = os.Open(*flProfilePath) + if err != nil { + return err + } + defer output.Close() + } } - defer pf.Close() var profile dep.Profile - if err := json.NewDecoder(pf).Decode(&profile); err != nil { + if err := json.NewDecoder(output).Decode(&profile); err != nil { return errors.Wrap(err, "decode DEP Profile JSON") } @@ -108,8 +116,7 @@ func printDEPProfileTemplate(anchorCerts []*x509.Certificate) error { anchorCertStr = string(jsonBytes) } - resp := fmt.Sprintf(` -{ + resp := fmt.Sprintf(`{ "profile_name": "(Required) Human readable name", "url": "https://mymdm.example.org/mdm/enroll", "allow_pairing": true, @@ -126,8 +133,7 @@ func printDEPProfileTemplate(anchorCerts []*x509.Certificate) error { "skip_setup_items": ["AppleID", "Android"], "department": "(Optional) support@example.com", "devices": ["SERIAL1","SERIAL2"] -} -`, anchorCertStr) +}`, anchorCertStr) fmt.Println(resp) return nil } diff --git a/cmd/mdmctl/get_dep_profiles.go b/cmd/mdmctl/get_dep_profiles.go index 24b2220e..12573c48 100644 --- a/cmd/mdmctl/get_dep_profiles.go +++ b/cmd/mdmctl/get_dep_profiles.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/json" "flag" "fmt" "os" @@ -21,27 +22,61 @@ func (out *depProfilesTableOutput) BasicFooter() { func (cmd *getCommand) getDEPProfiles(args []string) error { flagset := flag.NewFlagSet("dep-profiles", flag.ExitOnError) + flProfilePath := flagset.String("f", "", "filename of DEP profile to apply") flUUID := flagset.String("uuid", "", "DEP Profile UUID") flagset.Usage = usageFor(flagset, "mdmctl get dep-profiles [flags]") if err := flagset.Parse(args); err != nil { return err } - w := tabwriter.NewWriter(os.Stderr, 0, 4, 2, ' ', 0) - out := &depProfilesTableOutput{w} - out.BasicHeader() - defer out.BasicFooter() + ctx := context.Background() resp, err := cmd.list.GetDEPProfile(ctx, *flUUID) if err != nil { return err } - fmt.Fprintf(out.w, "%s\t%v\t%v\t%v\t%s\n", - resp.ProfileName, - resp.IsMandatory, - resp.IsMDMRemovable, - resp.AwaitDeviceConfigured, - strings.Join(resp.SkipSetupItems, ","), - ) + + if *flProfilePath == "" { + w := tabwriter.NewWriter(os.Stderr, 0, 4, 2, ' ', 0) + out := &depProfilesTableOutput{w} + out.BasicHeader() + defer out.BasicFooter() + + fmt.Fprintf(out.w, "%s\t%v\t%v\t%v\t%s\n", + resp.ProfileName, + resp.IsMandatory, + resp.IsMDMRemovable, + resp.AwaitDeviceConfigured, + strings.Join(resp.SkipSetupItems, ","), + ) + } else { + var output *os.File + { + if *flProfilePath == "-" { + output = os.Stdout + } else { + var err error + output, err = os.Create(*flProfilePath) + if err != nil { + return err + } + defer output.Close() + } + } + + // TODO: perhaps we want to store the raw DEP profile for storage + // as we may have problems with default/non-default values getting + // omitted in the marshalled JSON + enc := json.NewEncoder(output) + enc.SetIndent("", " ") + err = enc.Encode(resp) + if err != nil { + return err + } + + if *flProfilePath != "-" { + fmt.Printf("wrote DEP profile %s to: %s\n", *flUUID, *flProfilePath) + } + } return nil }