forked from micromdm/micromdm
/
get_app.go
62 lines (56 loc) · 1.46 KB
/
get_app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"os"
"text/tabwriter"
"github.com/micromdm/micromdm/platform/appstore"
)
type appsTableOutput struct{ w *tabwriter.Writer }
func (out *appsTableOutput) BasicHeader() {
fmt.Fprintf(out.w, "Name\tManifestURL\n")
}
func (out *appsTableOutput) BasicFooter() {
out.w.Flush()
}
func (cmd *getCommand) getApps(args []string) error {
flagset := flag.NewFlagSet("apps", flag.ExitOnError)
var (
flNameFilter = flagset.String("name", "", "specify the name of the app to get full details")
flOutputPath = flagset.String("f", "-", "path to save file to. defaults to stdout.")
)
flagset.Usage = usageFor(flagset, "mdmctl get apps [flags]")
if err := flagset.Parse(args); err != nil {
return err
}
ctx := context.Background()
apps, err := cmd.appsvc.ListApplications(ctx, appstore.ListAppsOption{
FilterName: []string{*flNameFilter},
})
if err != nil {
return err
}
if *flNameFilter != "" && (len(apps) != 0) {
payload := apps[0].Payload
if *flOutputPath == "-" {
fmt.Println(string(payload))
return nil
}
return ioutil.WriteFile(*flOutputPath, payload, 0644)
}
rURL, err := repoURL(cmd.config.ServerURL)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
out := appsTableOutput{w}
out.BasicHeader()
defer out.BasicFooter()
for _, a := range apps {
manifestURL := rURL + "/" + a.Name
fmt.Fprintf(out.w, "%s\t%s\n", a.Name, manifestURL)
}
return nil
}