• 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
/arbitrary/task/command.go
1
package task
2

3
import (
4
        "context"
5
        "os"
6
        "os/exec"
7
        "strings"
8

9
        "github.com/foomo/posh/pkg/command/tree"
10
        "github.com/foomo/posh/pkg/log"
11
        "github.com/foomo/posh/pkg/prompt/goprompt"
12
        "github.com/foomo/posh/pkg/readline"
13
        "github.com/foomo/posh/pkg/util/suggests"
14
        "github.com/pkg/errors"
15
        "github.com/pterm/pterm"
16
        "github.com/spf13/viper"
17
)
18

19
type (
20
        Command struct {
21
                l           log.Logger
22
                cfg         Config
23
                name        string
24
                configKey   string
25
                commandTree tree.Root
26
        }
27
        CommandOption func(*Command)
28
)
29

30
// ------------------------------------------------------------------------------------------------
31
// ~ Options
32
// ------------------------------------------------------------------------------------------------
33

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

40
func WithConfigKey(v string) CommandOption {
×
41
        return func(o *Command) {
×
42
                o.configKey = v
×
43
        }
×
44
}
45

46
// ------------------------------------------------------------------------------------------------
47
// ~ Constructor
48
// ------------------------------------------------------------------------------------------------
49

50
func NewCommand(l log.Logger, opts ...CommandOption) (*Command, error) {
×
51
        inst := &Command{
×
52
                l:         l.Named("task"),
×
53
                name:      "task",
×
54
                configKey: "task",
×
55
        }
×
56
        for _, opt := range opts {
×
57
                if opt != nil {
×
58
                        opt(inst)
×
59
                }
×
60
        }
61
        if err := viper.UnmarshalKey(inst.configKey, &inst.cfg); err != nil {
×
62
                return nil, err
×
63
        }
×
64

65
        inst.commandTree = tree.New(&tree.Node{
×
66
                Name:        inst.name,
×
67
                Description: "Run make scripts",
×
68
                Args: tree.Args{
×
69
                        {
×
70
                                Name: "task",
×
71
                                Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest {
×
72
                                        return suggests.List(inst.cfg.Names())
×
73
                                },
×
74
                        },
75
                },
76
                Execute: inst.execute,
77
        })
78

79
        return inst, nil
×
80
}
81

82
// ------------------------------------------------------------------------------------------------
83
// ~ Public methods
84
// ------------------------------------------------------------------------------------------------
85

86
func (c *Command) Name() string {
×
87
        return c.commandTree.Node().Name
×
88
}
×
89

90
func (c *Command) Description() string {
×
91
        return c.commandTree.Node().Description
×
92
}
×
93

94
func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest {
×
95
        return c.commandTree.Complete(ctx, r)
×
96
}
×
97

98
func (c *Command) Execute(ctx context.Context, r *readline.Readline) error {
×
99
        return c.commandTree.Execute(ctx, r)
×
100
}
×
101

102
func (c *Command) Help(ctx context.Context, r *readline.Readline) string {
×
103
        return c.commandTree.Help(ctx, r)
×
104
}
×
105

106
// ------------------------------------------------------------------------------------------------
107
// ~ Private methods
108
// ------------------------------------------------------------------------------------------------
109

110
func (c *Command) execute(ctx context.Context, r *readline.Readline) error {
×
111
        if err := c.executeTask(ctx, r.Args().At(0)); err != nil {
×
112
                return err
×
113
        }
×
NEW
114
        c.l.Success("🔧 | done")
×
115
        return nil
×
116
}
117

118
func (c *Command) executeTask(ctx context.Context, taskID string) error {
×
119
        ctx, cancel := context.WithCancel(ctx)
×
120
        defer cancel()
×
121

×
122
        task, ok := c.cfg[taskID]
×
123
        if !ok {
×
124
                return errors.Errorf("task not found: %s", taskID)
×
125
        }
×
126

127
        if task.Prompt != "" {
×
128
                if result, err := pterm.DefaultInteractiveConfirm.WithOnInterruptFunc(func() {
×
129
                        cancel()
×
130
                }).Show(task.Prompt); err != nil {
×
131
                        return err
×
132
                } else if ctx.Err() != nil {
×
133
                        return ctx.Err()
×
134
                } else if !result {
×
135
                        return nil
×
136
                }
×
137
        }
138

139
        for _, dep := range task.Deps {
×
140
                if ctx.Err() != nil {
×
141
                        return ctx.Err()
×
142
                }
×
143
                if err := c.executeTask(ctx, dep); err != nil {
×
144
                        return err
×
145
                }
×
146
        }
147

148
        for i, cmd := range task.Cmds {
×
149
                if ctx.Err() != nil {
×
150
                        return ctx.Err()
×
151
                }
×
152
                var sh *exec.Cmd
×
153
                if strings.HasPrefix(cmd, "sudo ") {
×
154
                        sh = exec.CommandContext(ctx, "sudo", "sh", "-c", strings.TrimPrefix(cmd, "sudo "))
×
155
                } else {
×
156
                        sh = exec.CommandContext(ctx, "sh", "-c", cmd)
×
157
                }
×
158
                sh.Stdin = os.Stdin
×
159
                sh.Stdout = os.Stdout
×
160
                sh.Stderr = os.Stderr
×
161
                sh.Env = os.Environ()
×
NEW
162
                c.l.Infof("🔧 | [%d|%d] %s: %s", i+1, len(task.Cmds), taskID, cmd)
×
163
                if err := sh.Run(); err != nil {
×
164
                        return err
×
165
                }
×
166
        }
167

168
        return nil
×
169
}
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

© 2025 Coveralls, Inc