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

foomo / posh-providers / 14327500583

08 Apr 2025 07:18AM UTC coverage: 0.0%. Remained the same
14327500583

Pull #199

github

web-flow
Merge 89f9e647e into f650b9cf6
Pull Request #199: feat(arbitrary/task): add command

0 of 95 new or added lines in 3 files covered. (0.0%)

0 of 8973 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

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

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

28
// ------------------------------------------------------------------------------------------------
29
// ~ Options
30
// ------------------------------------------------------------------------------------------------
31

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

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

44
// ------------------------------------------------------------------------------------------------
45
// ~ Constructor
46
// ------------------------------------------------------------------------------------------------
47

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

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

NEW
77
        return inst, nil
×
78
}
79

80
// ------------------------------------------------------------------------------------------------
81
// ~ Public methods
82
// ------------------------------------------------------------------------------------------------
83

NEW
84
func (c *Command) Name() string {
×
NEW
85
        return c.commandTree.Node().Name
×
NEW
86
}
×
87

NEW
88
func (c *Command) Description() string {
×
NEW
89
        return c.commandTree.Node().Description
×
NEW
90
}
×
91

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

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

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

104
// ------------------------------------------------------------------------------------------------
105
// ~ Private methods
106
// ------------------------------------------------------------------------------------------------
107

NEW
108
func (c *Command) execute(ctx context.Context, r *readline.Readline) error {
×
NEW
109
        return c.executeTask(ctx, r.Args().At(0))
×
NEW
110
}
×
111

NEW
112
func (c *Command) executeTask(ctx context.Context, taskID string) error {
×
NEW
113
        task, ok := c.cfg[taskID]
×
NEW
114
        if !ok {
×
NEW
115
                return errors.Errorf("task not found: %s", taskID)
×
NEW
116
        }
×
117

NEW
118
        if task.Prompt != "" {
×
NEW
119
                if result, err := pterm.DefaultInteractiveConfirm.Show(task.Prompt); err != nil {
×
NEW
120
                        return err
×
NEW
121
                } else if !result {
×
NEW
122
                        return nil
×
NEW
123
                }
×
124
        }
125

NEW
126
        for _, dep := range task.Deps {
×
NEW
127
                if err := c.executeTask(ctx, dep); err != nil {
×
NEW
128
                        return err
×
NEW
129
                }
×
130
        }
131

NEW
132
        for _, cmd := range task.Cmds {
×
NEW
133
                if err := shell.New(ctx, c.l, cmd).Debug().Run(); err != nil {
×
NEW
134
                        return err
×
NEW
135
                }
×
136
        }
137

NEW
138
        return nil
×
139
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc