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

foomo / posh / 14267892308

04 Apr 2025 02:41PM UTC coverage: 6.101% (-0.07%) from 6.172%
14267892308

push

github

web-flow
Merge pull request #110 from foomo/feature/v0.11.0

feat: v0.11.0

3 of 122 new or added lines in 9 files covered. (2.46%)

5 existing lines in 2 files now uncovered.

159 of 2606 relevant lines covered (6.1%)

0.71 hits per line

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

0.0
/pkg/command/env.go
1
package command
2

3
import (
4
        "context"
5
        "os"
6
        "sort"
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/pterm/pterm"
14
)
15

16
type Env struct {
17
        l    log.Logger
18
        tree tree.Root
19
}
20

21
// ------------------------------------------------------------------------------------------------
22
// ~ Constructor
23
// ------------------------------------------------------------------------------------------------
24

25
func NewEnv(l log.Logger) *Env {
×
26
        inst := &Env{
×
27
                l: l,
×
28
        }
×
29
        inst.tree = tree.New(&tree.Node{
×
30
                Name:        "env",
×
31
                Description: "Manage internal environment variables",
×
32
                Nodes: tree.Nodes{
×
33
                        {
×
34
                                Name:        "list",
×
35
                                Description: "List all environment variables",
×
36
                                Execute:     inst.list,
×
37
                        },
×
38
                        {
×
39
                                Name:        "set",
×
40
                                Description: "Set an internal environment variable",
×
41
                                Args: tree.Args{
×
42
                                        {
×
43
                                                Name:        "Key",
×
44
                                                Description: "Key of the environment variable.",
×
45
                                        },
×
46
                                        {
×
47
                                                Name:        "Value",
×
48
                                                Optional:    true,
×
49
                                                Description: "Value of the environment variable.",
×
50
                                        },
×
51
                                },
×
52
                                Execute: inst.set,
×
53
                        },
×
54
                        {
×
55
                                Name:        "unset",
×
56
                                Description: "Unset an environment variable",
×
57
                                Args: tree.Args{
×
58
                                        {
×
59
                                                Name:        "Key",
×
60
                                                Description: "Key of the environment variable.",
×
61
                                        },
×
62
                                },
×
63
                                Execute: inst.unset,
×
64
                        },
×
65
                },
×
66
        })
×
67
        return inst
×
68
}
×
69

70
// ------------------------------------------------------------------------------------------------
71
// ~ Public methods
72
// ------------------------------------------------------------------------------------------------
73

74
func (c *Env) Name() string {
×
75
        return c.tree.Node().Name
×
76
}
×
77

78
func (c *Env) Description() string {
×
79
        return c.tree.Node().Description
×
80
}
×
81

82
func (c *Env) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest {
×
83
        return c.tree.Complete(ctx, r)
×
84
}
×
85

86
func (c *Env) Execute(ctx context.Context, r *readline.Readline) error {
×
87
        return c.tree.Execute(ctx, r)
×
88
}
×
89

90
func (c *Env) Help(ctx context.Context, r *readline.Readline) string {
×
91
        return c.tree.Help(ctx, r)
×
92
}
×
93

94
// ------------------------------------------------------------------------------------------------
95
// ~ Private methods
96
// ------------------------------------------------------------------------------------------------
97

98
func (c *Env) set(ctx context.Context, r *readline.Readline) error {
×
99
        return os.Setenv(r.Args().At(1), r.Args().AtDefault(2, ""))
×
100
}
×
101

102
func (c *Env) unset(ctx context.Context, r *readline.Readline) error {
×
103
        return os.Unsetenv(r.Args().At(1))
×
104
}
×
105

106
func (c *Env) list(ctx context.Context, r *readline.Readline) error {
×
107
        data := pterm.TableData{{"Name", "Value"}}
×
108
        values := os.Environ()
×
109
        sort.Strings(values)
×
NEW
110
        var pairs [][]string
×
111
        for _, s := range values {
×
NEW
112
                pairs = append(pairs, strings.SplitN(s, "=", 2))
×
113
        }
×
NEW
114
        var maxKeyLen int
×
NEW
115
        for _, pair := range pairs {
×
NEW
116
                maxKeyLen = max(maxKeyLen, len(pair[0]))
×
NEW
117
        }
×
NEW
118
        maxValueLen := pterm.GetTerminalWidth() - maxKeyLen - 5
×
NEW
119
        for i, pair := range pairs {
×
NEW
120
                var value string
×
NEW
121
                for len(pair[1]) > maxValueLen {
×
NEW
122
                        value += pair[1][:maxValueLen] + "\n"
×
NEW
123
                        pair[1] = pair[1][maxValueLen:]
×
NEW
124
                }
×
NEW
125
                pairs[i][1] = value + pair[1]
×
126
        }
NEW
127
        data = append(data, pairs...)
×
NEW
128
        return pterm.DefaultTable.WithHasHeader(true).WithHeaderRowSeparator("-").WithData(data).Render()
×
129
}
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