-
Notifications
You must be signed in to change notification settings - Fork 9
/
ls.go
116 lines (107 loc) · 3.37 KB
/
ls.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cmd
import (
"context"
"fmt"
"github.com/kr/text"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/console/colors"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
)
func NewLsCmd() *cobra.Command {
var (
env cfg.Context
locs fncobra.Locations
details bool
)
return fncobra.Cmd(
&cobra.Command{
Use: "ls [path/to/package | module/path/...]",
Short: "List packages or objects defined in a package.",
Aliases: []string{"list"},
}).
WithFlags(func(flags *pflag.FlagSet) {
flags.BoolVarP(&details, "details", "l", false, "If set to true, prints the contents of the listed packages.")
}).
With(
fncobra.HardcodeEnv(&env, "dev"),
fncobra.ParseLocations(&locs, &env, fncobra.ParseLocationsOpts{ReturnAllIfNoneSpecified: true})).
Do(func(ctx context.Context) error {
style := colors.Ctx(ctx)
stdout := console.Stdout(ctx)
pl := parsing.NewPackageLoader(env)
for _, l := range locs.Locations {
fmt.Fprintf(stdout, "%s\n", l)
if locs.UserSpecified || details {
pkg, err := pl.LoadByName(ctx, l.AsPackageName())
if err != nil {
return err
}
resout := text.NewIndentWriter(stdout, []byte(" "))
if pkg.Extension != nil {
fmt.Fprintf(resout, "%s\n", style.Comment.Apply("Extension"))
}
if pkg.Service != nil {
fmt.Fprintf(resout, "%s\n", style.Comment.Apply("Service"))
}
if pkg.Server != nil {
fmt.Fprintf(resout, "%s\n", style.Comment.Apply("Server"))
}
for _, r := range pkg.Tests {
fmt.Fprintf(resout, "%s :%s\n",
style.Comment.Apply("Test"),
r.Name)
}
for _, r := range pkg.Binaries {
fmt.Fprintf(resout, "%s :%s\n",
style.Comment.Apply("Binary"),
r.Name)
}
for _, r := range pkg.Secrets {
fmt.Fprintf(resout, "%s :%s %s\n",
style.Comment.Apply("Secret"),
r.Name,
style.Comment.Apply(fmt.Sprintf("(%s)", r.Description)))
}
for _, r := range pkg.Volumes {
fmt.Fprintf(resout, "%s :%s (%s) <- %s\n",
style.Comment.Apply("Volume"),
r.Name,
r.Kind,
r.Owner)
}
for _, r := range pkg.Resources {
fmt.Fprintf(resout, "%s :%s (%s)\n",
style.Comment.Apply("Resource"),
r.ResourceRef.Name,
formatPkgRef(style, r.Spec.Class.Ref))
fmt.Fprintf(resout, " %s %s\n",
style.Comment.Apply("<-"),
r.Spec.Provider.Spec.PackageName)
}
for _, r := range pkg.ResourceClasses {
fmt.Fprintf(resout, "%s :%s\n",
style.Comment.Apply("ResourceClass"),
r.Ref.Name)
}
for _, r := range pkg.ResourceProviders {
fmt.Fprintf(resout, "%s %s\n",
style.Comment.Apply("ResourceProvider"),
formatPkgRef(style, r.Spec.ProvidesClass))
}
fmt.Fprintln(resout)
}
}
return nil
})
}
func formatPkgRef(style colors.Style, pr *schema.PackageRef) string {
return fmt.Sprintf("%s:%s", style.LessRelevant.Apply(pr.PackageName), pr.Name)
}