-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpull.go
More file actions
82 lines (70 loc) · 2.17 KB
/
pull.go
File metadata and controls
82 lines (70 loc) · 2.17 KB
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
package cmd
import (
"os"
"github.com/cloudentity/cac/internal/cac"
"github.com/cloudentity/cac/internal/cac/api"
"github.com/cloudentity/cac/internal/cac/utils"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/exp/slog"
)
var (
pullCmd = &cobra.Command{
Use: "pull",
Short: "Pull existing configuration",
RunE: func(cmd *cobra.Command, args []string) error {
var (
app *cac.Application
data api.Patch
err error
)
if app, err = cac.InitApp(rootConfig.ConfigPath, rootConfig.Profile, rootConfig.Tenant); err != nil {
return err
}
slog.
With("workspace", rootConfig.Workspace).
With("tenant", rootConfig.Tenant).
With("filters", pullConfig.Filters).
With("config", rootConfig.ConfigPath).
Info("Pulling configuration")
if data, err = app.Client.Read(
cmd.Context(),
api.WithWorkspace(rootConfig.Workspace),
api.WithSecrets(pullConfig.WithSecrets),
api.WithFilters(pullConfig.Filters),
); err != nil {
return err
}
if pullConfig.Out == "" {
// default
if err = app.Storage.Write(cmd.Context(), data, api.WithWorkspace(rootConfig.Workspace), api.WithSecrets(pullConfig.WithSecrets)); err != nil {
return err
}
} else {
bts, err := utils.ToYaml(data)
if err != nil {
return errors.Wrap(err, "failed to marshal data to YAML")
}
if pullConfig.Out == "-" {
if _, err = os.Stdout.Write(bts); err != nil {
return errors.Wrap(err, "failed to write diff result to stdout")
}
} else if err = os.WriteFile(pullConfig.Out, bts, 0644); err != nil {
return errors.Wrap(err, "failed to write diff result to file")
}
}
slog.Info("Configuration pulled", "out", pullConfig.Out)
return nil
},
}
pullConfig struct {
WithSecrets bool
Filters []string
Out string
}
)
func init() {
pullCmd.PersistentFlags().BoolVar(&pullConfig.WithSecrets, "with-secrets", false, "Pull secrets")
pullCmd.PersistentFlags().StringSliceVar(&pullConfig.Filters, "filter", []string{}, "Pull only selected resources")
pullCmd.PersistentFlags().StringVar(&pullConfig.Out, "out", "", "Pull output. It can be a file or '-' for stdout")
}