-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add env command * fix: add pad * fix: hostname separator * feat: remove enforced $H * refactor: rename pkg to internal * revert: hostname separation
- Loading branch information
1 parent
71afc97
commit 8929c62
Showing
12 changed files
with
138 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package command | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"strings" | ||
|
||
"github.com/foomo/posh/pkg/command/tree" | ||
"github.com/foomo/posh/pkg/log" | ||
"github.com/foomo/posh/pkg/prompt/goprompt" | ||
"github.com/foomo/posh/pkg/readline" | ||
"github.com/pterm/pterm" | ||
) | ||
|
||
type Env struct { | ||
l log.Logger | ||
tree tree.Root | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------ | ||
// ~ Constructor | ||
// ------------------------------------------------------------------------------------------------ | ||
|
||
func NewEnv(l log.Logger) *Env { | ||
inst := &Env{ | ||
l: l, | ||
} | ||
inst.tree = tree.New(&tree.Node{ | ||
Name: "env", | ||
Description: "Manage internal environment variables", | ||
Nodes: tree.Nodes{ | ||
{ | ||
Name: "list", | ||
Description: "List all environment variables", | ||
Execute: inst.list, | ||
}, | ||
{ | ||
Name: "set", | ||
Description: "Set an internal environment variable", | ||
Args: tree.Args{ | ||
{ | ||
Name: "Key", | ||
Description: "Key of the environment variable.", | ||
}, | ||
{ | ||
Name: "Value", | ||
Optional: true, | ||
Description: "Value of the environment variable.", | ||
}, | ||
}, | ||
Execute: inst.set, | ||
}, | ||
{ | ||
Name: "unset", | ||
Description: "Unset an environment variable", | ||
Args: tree.Args{ | ||
{ | ||
Name: "Key", | ||
Description: "Key of the environment variable.", | ||
}, | ||
}, | ||
Execute: inst.unset, | ||
}, | ||
}, | ||
}) | ||
return inst | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------ | ||
// ~ Public methods | ||
// ------------------------------------------------------------------------------------------------ | ||
|
||
func (c *Env) Name() string { | ||
return c.tree.Node().Name | ||
} | ||
|
||
func (c *Env) Description() string { | ||
return c.tree.Node().Description | ||
} | ||
|
||
func (c *Env) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { | ||
return c.tree.Complete(ctx, r) | ||
} | ||
|
||
func (c *Env) Execute(ctx context.Context, r *readline.Readline) error { | ||
return c.tree.Execute(ctx, r) | ||
} | ||
|
||
func (c *Env) Help(ctx context.Context, r *readline.Readline) string { | ||
return c.tree.Help(ctx, r) | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------ | ||
// ~ Private methods | ||
// ------------------------------------------------------------------------------------------------ | ||
|
||
func (c *Env) set(ctx context.Context, r *readline.Readline) error { | ||
return os.Setenv(r.Args().At(0), r.Args().AtDefault(1, "")) | ||
} | ||
|
||
func (c *Env) unset(ctx context.Context, r *readline.Readline) error { | ||
return os.Unsetenv(r.Args().At(0)) | ||
} | ||
|
||
func (c *Env) list(ctx context.Context, r *readline.Readline) error { | ||
data := pterm.TableData{{"Name", "Value"}} | ||
for _, s := range os.Environ() { | ||
data = append(data, strings.SplitN(s, "=", 2)) | ||
} | ||
return pterm.DefaultTable.WithHasHeader(true).WithData(data).Render() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters