• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

foomo / posh-providers / 16795317556

07 Aug 2025 04:44AM UTC coverage: 0.0%. Remained the same
16795317556

push

github

web-flow
Merge pull request #219 from foomo/feature/doctl

feat(digitalocean/doctl): extend command

0 of 30 new or added lines in 2 files covered. (0.0%)

1 existing line in 1 file now uncovered.

0 of 9196 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/digitalocean/doctl/command.go
1
package doctl
2

3
import (
4
        "context"
5

6
        "github.com/foomo/posh-providers/kubernets/kubectl"
7
        "github.com/foomo/posh/pkg/cache"
8
        "github.com/foomo/posh/pkg/command/tree"
9
        "github.com/foomo/posh/pkg/log"
10
        "github.com/foomo/posh/pkg/prompt/goprompt"
11
        "github.com/foomo/posh/pkg/readline"
12
        "github.com/foomo/posh/pkg/shell"
13
        "github.com/foomo/posh/pkg/util/suggests"
14
        "github.com/pkg/errors"
15
)
16

17
type (
18
        Command struct {
19
                l             log.Logger
20
                name          string
21
                doctl         *Doctl
22
                cache         cache.Cache
23
                kubectl       *kubectl.Kubectl
24
                commandTree   tree.Root
25
                clusterNameFn ClusterNameFn
26
        }
27
        ClusterNameFn func(name string, cluster Cluster) string
28
        CommandOption func(*Command)
29
)
30

31
// ------------------------------------------------------------------------------------------------
32
// ~ Options
33
// ------------------------------------------------------------------------------------------------
34

35
func CommandWithName(v string) CommandOption {
×
36
        return func(o *Command) {
×
37
                o.name = v
×
38
        }
×
39
}
40

41
func CommandWithClusterNameFn(v ClusterNameFn) CommandOption {
×
42
        return func(o *Command) {
×
43
                o.clusterNameFn = v
×
44
        }
×
45
}
46

47
// ------------------------------------------------------------------------------------------------
48
// ~ Constructor
49
// ------------------------------------------------------------------------------------------------
50

51
func NewCommand(l log.Logger, cache cache.Cache, doctl *Doctl, kubectl *kubectl.Kubectl, opts ...CommandOption) *Command {
×
52
        inst := &Command{
×
53
                l:       l.Named("doctl"),
×
54
                name:    "doctl",
×
55
                cache:   cache,
×
56
                doctl:   doctl,
×
57
                kubectl: kubectl,
×
58
                clusterNameFn: func(name string, cluster Cluster) string {
×
59
                        return name
×
60
                },
×
61
        }
62
        for _, opt := range opts {
×
63
                if opt != nil {
×
64
                        opt(inst)
×
65
                }
×
66
        }
67

68
        inst.commandTree = tree.New(&tree.Node{
×
69
                Name:        inst.name,
×
70
                Description: "Manage digital ocean resources",
×
71
                Nodes: tree.Nodes{
×
72
                        {
×
NEW
73
                                Name:        "auth",
×
NEW
74
                                Description: "Manage authentication",
×
NEW
75
                                Nodes: tree.Nodes{
×
NEW
76
                                        {
×
NEW
77
                                                Name:        "init",
×
NEW
78
                                                Description: "Initialize doctl to use a specific account",
×
NEW
79
                                                Execute:     inst.exec,
×
NEW
80
                                        },
×
NEW
81
                                },
×
NEW
82
                                Execute: inst.exec,
×
83
                        },
×
84
                        {
×
NEW
85
                                Name:        "registry",
×
NEW
86
                                Description: "Manage container registries",
×
NEW
87
                                Nodes: tree.Nodes{
×
88
                                        {
×
NEW
89
                                                Name:        "login",
×
NEW
90
                                                Description: "Log in Docker to a container registry",
×
NEW
91
                                                Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error {
×
NEW
92
                                                        fs.Default().Bool("never-expire", false, "Never expire the credentials")
×
NEW
93
                                                        return nil
×
UNCOV
94
                                                },
×
95
                                                Execute: inst.exec,
96
                                        },
97
                                        {
98
                                                Name:        "logout",
99
                                                Description: "Log out Docker from a container registry",
100
                                                Execute:     inst.exec,
101
                                        },
102
                                },
103
                                Execute: inst.exec,
104
                        },
105
                        {
106
                                Name:        "kubernetes",
107
                                Description: "Manage Kubernetes clusters",
108
                                Nodes: tree.Nodes{
109
                                        {
110
                                                Name:        "kubeconfig",
111
                                                Description: "Retrieve credentials to access remote cluster.",
112
                                                Args: tree.Args{
113
                                                        {
114
                                                                Name:        "cluster",
115
                                                                Description: "Name of the cluster.",
NEW
116
                                                                Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest {
×
NEW
117
                                                                        return suggests.List(inst.doctl.cfg.ClusterNames())
×
NEW
118
                                                                },
×
119
                                                        },
120
                                                },
NEW
121
                                                Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error {
×
NEW
122
                                                        fs.Internal().String("profile", "", "Store credentials in given profile.")
×
NEW
123
                                                        return fs.Internal().SetValues("profile", "digitalocean")
×
NEW
124
                                                },
×
125
                                                Execute: inst.kubeconfig,
126
                                        },
127
                                },
128
                        },
129
                },
130
        })
131

132
        return inst
×
133
}
134

135
// ------------------------------------------------------------------------------------------------
136
// ~ Public methods
137
// ------------------------------------------------------------------------------------------------
138

139
func (c *Command) Name() string {
×
140
        return c.commandTree.Node().Name
×
141
}
×
142

143
func (c *Command) Description() string {
×
144
        return c.commandTree.Node().Description
×
145
}
×
146

147
func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest {
×
148
        return c.commandTree.Complete(ctx, r)
×
149
}
×
150

151
func (c *Command) Execute(ctx context.Context, r *readline.Readline) error {
×
152
        return c.commandTree.Execute(ctx, r)
×
153
}
×
154

155
func (c *Command) Help(ctx context.Context, r *readline.Readline) string {
×
156
        return c.commandTree.Help(ctx, r)
×
157
}
×
158

159
// ------------------------------------------------------------------------------------------------
160
// ~ Private methods
161
// ------------------------------------------------------------------------------------------------
162

163
func (c *Command) kubeconfig(ctx context.Context, r *readline.Readline) error {
×
164
        var args []string
×
165
        ifs := r.FlagSets().Internal()
×
166
        clusterName := r.Args().At(1)
×
167

×
168
        cluster, err := c.doctl.cfg.Cluster(clusterName)
×
169
        if err != nil {
×
170
                return errors.Errorf("failed to retrieve cluster for: %q", clusterName)
×
171
        }
×
172

173
        kubectlCluster := c.kubectl.Cluster(c.clusterNameFn(clusterName, cluster))
×
174
        if kubectlCluster == nil {
×
175
                return errors.Errorf("failed to retrieve kubectl cluster for: %q", cluster.Name)
×
176
        }
×
177

178
        profile, err := ifs.GetString("profile")
×
179
        if err != nil {
×
180
                return err
×
181
        }
×
182

183
        return shell.New(ctx, c.l, "doctl", "kubernetes", "cluster", "kubeconfig", "save", cluster.Name).
×
184
                Args(args...).
×
185
                Args(r.AdditionalArgs()...).
×
186
                Env(kubectlCluster.Env(profile)).
×
187
                Run()
×
188
}
189

NEW
190
func (c *Command) exec(ctx context.Context, r *readline.Readline) error {
×
NEW
191
        return shell.New(ctx, c.l, "doctl").
×
NEW
192
                Args(r.Args()...).
×
193
                Args(r.Flags()...).
×
194
                Args(r.AdditionalArgs()...).
×
195
                Run()
×
196
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc