This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
clear.go
67 lines (52 loc) · 1.55 KB
/
clear.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package secrethub
import (
"fmt"
"os"
"github.com/secrethub/secrethub-cli/internals/cli"
"github.com/secrethub/secrethub-cli/internals/cli/ui"
"github.com/secrethub/secrethub-cli/internals/secretspec"
)
// ClearCommand clears the secrets from the system.
type ClearCommand struct {
in string
io ui.IO
}
// NewClearCommand creates a new ClearCommand.
func NewClearCommand(io ui.IO) *ClearCommand {
return &ClearCommand{
io: io,
}
}
// Register registers the command, arguments and flags on the provided Registerer.
func (cmd *ClearCommand) Register(r cli.Registerer) {
clause := r.Command("clear", "Clear the secrets from your local environment. This reads and parses the secrets.yml file in the current working directory.").Hidden()
clause.Flags().StringVarP(&cmd.in, "in", "i", "secrets.yml", "The path to a secrets.yml file to read")
clause.BindAction(cmd.Run)
clause.BindArguments(nil)
}
// Run clears the secrets from the system.
func (cmd *ClearCommand) Run() error {
presenter, err := secretspec.NewPresenter("", true, secretspec.DefaultParsers...)
if err != nil {
return err
}
_, err = os.Stat(cmd.in)
if os.IsNotExist(err) {
return ErrFileNotFound(cmd.in)
}
spec, err := os.ReadFile(cmd.in)
if err != nil {
return ErrCannotReadFile(cmd.in, err)
}
err = presenter.Parse(spec)
if err != nil {
return err
}
fmt.Fprintln(cmd.io.Output(), "Clearing secrets...")
err = presenter.Clear()
if err != nil {
return err
}
fmt.Fprintf(cmd.io.Output(), "Clear complete! The secrets are no longer available on the system.\n")
return nil
}