Skip to content

Commit 6f81b6f

Browse files
committed
Replace logrus with slog
1 parent c68d820 commit 6f81b6f

21 files changed

Lines changed: 147 additions & 118 deletions

File tree

cmd/hackenv/main.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
package main
33

44
import (
5-
"os"
6-
75
"github.com/alecthomas/kong"
8-
log "github.com/sirupsen/logrus"
96

107
"github.com/eikendev/hackenv/internal/commands"
8+
"github.com/eikendev/hackenv/internal/logging"
119
"github.com/eikendev/hackenv/internal/options"
1210
)
1311

@@ -23,23 +21,15 @@ var cmd struct {
2321
Version commands.VersionCommand `cmd:"version" help:"Print the version of hackenv"`
2422
}
2523

26-
func init() {
27-
log.SetOutput(os.Stderr)
28-
log.SetLevel(log.InfoLevel)
29-
log.SetFormatter(&log.TextFormatter{
30-
DisableTimestamp: true,
31-
})
32-
}
33-
3424
func main() {
35-
ctx := kong.Parse(&cmd)
25+
kctx := kong.Parse(&cmd,
26+
kong.Description("hackenv provisions and manages preconfigured VMs for security research."),
27+
kong.UsageOnError(),
28+
kong.Bind(&cmd.Options),
29+
)
3630

37-
if cmd.Verbose {
38-
log.SetLevel(log.DebugLevel)
39-
}
31+
logging.Setup(cmd.Verbose)
4032

41-
err := ctx.Run(&cmd.Options)
42-
if err != nil {
43-
os.Exit(1)
44-
}
33+
err := kctx.Run()
34+
kctx.FatalIfErrorf(err)
4535
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ require (
88
github.com/fatih/color v1.18.0
99
github.com/melbahja/goph v1.4.0
1010
github.com/schollz/progressbar/v3 v3.18.0
11-
github.com/sirupsen/logrus v1.9.3
1211
libvirt.org/go/libvirt v1.11006.0
1312
)
1413

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
3838
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
3939
github.com/schollz/progressbar/v3 v3.18.0 h1:uXdoHABRFmNIjUfte/Ex7WtuyVslrw2wVPQmCN62HpA=
4040
github.com/schollz/progressbar/v3 v3.18.0/go.mod h1:IsO3lpbaGuzh8zIMzgY3+J8l4C8GjO0Y9S69eFvNsec=
41-
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
42-
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
4341
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4442
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
4543
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
@@ -65,7 +63,6 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
6563
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6664
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6765
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
68-
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6966
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7067
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7168
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

internal/banner/banner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package banner
33

44
import (
55
"fmt"
6+
"log/slog"
67
"os"
78

89
"github.com/fatih/color"
9-
log "github.com/sirupsen/logrus"
1010
)
1111

1212
const banner = `
@@ -24,7 +24,7 @@ func PrintBanner() {
2424

2525
_, err := color.New(color.FgBlue).Fprintln(os.Stderr, " @eikendev")
2626
if err != nil {
27-
log.Warnf("Cannot print banner: %s", err)
27+
slog.Warn("Cannot print banner", "err", err)
2828
}
2929

3030
fmt.Fprintln(os.Stderr, "")

internal/commands/down.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package commands
22

33
import (
4-
log "github.com/sirupsen/logrus"
4+
"log/slog"
55

66
"github.com/eikendev/hackenv/internal/handling"
77
"github.com/eikendev/hackenv/internal/images"
@@ -24,7 +24,7 @@ func (c *DownCommand) Run(s *options.Options) error {
2424

2525
err := dom.Destroy()
2626
if err != nil {
27-
log.Errorf("Cannot destroy domain: %s\n", err)
27+
slog.Error("Cannot destroy domain", "err", err)
2828
return err
2929
}
3030

internal/commands/fix.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package commands
22

33
import (
44
"fmt"
5+
"log/slog"
56
"os/exec"
67
"strings"
78

8-
log "github.com/sirupsen/logrus"
9-
109
"github.com/eikendev/hackenv/internal/options"
1110
"github.com/eikendev/hackenv/internal/scripts"
1211
)
@@ -47,7 +46,7 @@ func (c *all) Run(s *options.Options) error {
4746
func execCommand(scripts []string, verbose bool) error {
4847
for i, script := range scripts {
4948
cmd := exec.Command("bash")
50-
log.Infof("Running script %v/%v...\n\n", i, len(scripts)-1)
49+
slog.Info("Running script", "index", i+1, "total", len(scripts))
5150
cmd.Stdin = strings.NewReader(script)
5251
b, err := cmd.CombinedOutput()
5352
if verbose {

internal/commands/get.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"crypto/sha256"
55
"fmt"
66
"io"
7+
"log/slog"
78
"net/http"
89
"os"
910

1011
progressbar "github.com/schollz/progressbar/v3"
11-
log "github.com/sirupsen/logrus"
1212

1313
"github.com/eikendev/hackenv/internal/handling"
1414
"github.com/eikendev/hackenv/internal/images"
@@ -23,18 +23,18 @@ type GetCommand struct {
2323

2424
// https://golang.org/pkg/crypto/sha256/#example_New_file
2525
func calculateFileChecksum(path string) (string, error) {
26-
log.Printf("Calculating checksum of %s\n", path)
26+
slog.Info("Calculating checksum", "path", path)
2727

2828
f, err := os.Open(path) //#nosec G304
2929
if err != nil {
30-
log.Errorf("Failed to open file: %s\n", err)
30+
slog.Error("Failed to open file", "path", path, "err", err)
3131
return "", err
3232
}
3333
defer handling.Close(f)
3434

3535
h := sha256.New()
3636
if _, err := io.Copy(h, f); err != nil {
37-
log.Errorf("Failed to copy file content: %s\n", err)
37+
slog.Error("Failed to copy file content", "path", path, "err", err)
3838
return "", err
3939
}
4040

@@ -43,18 +43,18 @@ func calculateFileChecksum(path string) (string, error) {
4343

4444
// https://stackoverflow.com/a/11693049
4545
func downloadImage(path, url string) error {
46-
log.Printf("Downloading image to %s\n", path)
46+
slog.Info("Downloading image", "path", path, "url", url)
4747

4848
out, err := os.Create(path) //#nosec G304
4949
if err != nil {
50-
log.Errorf("Cannot write image file: %s\n", err)
50+
slog.Error("Cannot create image file", "path", path, "err", err)
5151
return err
5252
}
5353
defer handling.Close(out)
5454

5555
resp, err := http.Get(url) //#nosec G107
5656
if err != nil {
57-
log.Errorf("Cannot download image file: %s\n", err)
57+
slog.Error("Cannot download image file", "url", url, "err", err)
5858
return err
5959
}
6060
if resp == nil {
@@ -63,7 +63,7 @@ func downloadImage(path, url string) error {
6363
defer handling.Close(resp.Body)
6464

6565
if resp.StatusCode != http.StatusOK {
66-
log.Errorf("Cannot download image file: bad status %s\n", resp.Status)
66+
slog.Error("Cannot download image file: bad status", "status", resp.Status)
6767
return err
6868
}
6969

@@ -74,11 +74,11 @@ func downloadImage(path, url string) error {
7474

7575
_, err = io.Copy(io.MultiWriter(out, bar), resp.Body)
7676
if err != nil {
77-
log.Errorf("Cannot write image file: %s\n", err)
77+
slog.Error("Cannot write image file", "path", path, "err", err)
7878
return err
7979
}
8080

81-
log.Println("Download successful")
81+
slog.Info("Download successful")
8282
return nil
8383
}
8484

@@ -89,17 +89,17 @@ func validateChecksum(localPath, checksum string) error {
8989
}
9090

9191
if newChecksum != checksum {
92-
checksumMsg := fmt.Sprintf("Downloaded image has bad checksum: %s instead of %s", newChecksum, checksum)
93-
9492
err := os.Remove(localPath)
9593
if err != nil {
96-
log.Fatalf("%s. Unable to remove file.\n", checksumMsg)
94+
slog.Error("Downloaded image has bad checksum and cannot be removed", "path", localPath, "expected", checksum, "actual", newChecksum, "err", err)
95+
os.Exit(1)
9796
}
9897

99-
log.Fatalf("%s. File removed.\n", checksumMsg)
98+
slog.Error("Downloaded image has bad checksum and was removed", "path", localPath, "expected", checksum, "actual", newChecksum)
99+
os.Exit(1)
100100
}
101101

102-
log.Println("Checksum validated successfully")
102+
slog.Info("Checksum validated successfully")
103103
return nil
104104
}
105105

@@ -111,7 +111,7 @@ func (c *GetCommand) Run(s *options.Options) error {
111111
return fmt.Errorf("failed to get download information")
112112
}
113113

114-
log.Printf("Found file %s with checksum %s\n", info.Filename, info.Checksum)
114+
slog.Info("Found image to download", "filename", info.Filename, "checksum", info.Checksum)
115115

116116
localPath := image.GetLocalPath(info.Version)
117117

@@ -120,18 +120,19 @@ func (c *GetCommand) Run(s *options.Options) error {
120120
// The image already exists.
121121

122122
if !c.Update && !c.Force {
123-
log.Println("An image is already installed; update with --update")
123+
slog.Info("An image is already installed; use --update to refresh")
124124
return nil
125125
}
126126

127127
localVersion := image.FileVersion(localPath)
128128

129129
if !c.Force && image.VersionComparer.Eq(info.Version, localVersion) {
130-
log.Println("Latest image is already installed; force with --force")
130+
slog.Info("Latest image is already installed; use --force to overwrite")
131131
return nil
132132
}
133133
} else if !os.IsNotExist(err) {
134-
log.Fatalf("Unable to get file information for path %s\n", localPath)
134+
slog.Error("Unable to get file information", "path", localPath, "err", err)
135+
os.Exit(1)
135136
}
136137

137138
err := downloadImage(localPath, image.ArchiveURL+"/"+info.Filename)
@@ -144,7 +145,7 @@ func (c *GetCommand) Run(s *options.Options) error {
144145
return err
145146
}
146147

147-
log.Info("When using SELinux, don't forget to label the image with the fix command before proceeding")
148+
slog.Info("When using SELinux, label the image with the fix command before proceeding")
148149

149150
return nil
150151
}

internal/commands/gui.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package commands
22

33
import (
44
"fmt"
5+
"log/slog"
56
"os"
67
"os/exec"
78

8-
log "github.com/sirupsen/logrus"
9-
109
"github.com/eikendev/hackenv/internal/constants"
1110
"github.com/eikendev/hackenv/internal/handling"
1211
"github.com/eikendev/hackenv/internal/images"
@@ -58,13 +57,13 @@ func (c *GuiCommand) Run(s *options.Options) error {
5857
"SPICE://localhost",
5958
}
6059
} else {
61-
log.Errorf("Unable to locate %s to connect to the VM.\n", c.Viewer)
60+
slog.Error("Unable to locate viewer to connect to the VM", "viewer", c.Viewer)
6261
return fmt.Errorf("unable to locate %s to connect to the VM", c.Viewer)
6362
}
6463

6564
cwd, err := os.Getwd()
6665
if err != nil {
67-
log.Printf("Cannot get current working directory: %s\n", err)
66+
slog.Warn("Cannot get current working directory", "err", err)
6867
}
6968

7069
cmd := exec.Command(args[0], args[1:]...) //#nosec G204
@@ -73,7 +72,7 @@ func (c *GuiCommand) Run(s *options.Options) error {
7372

7473
err = cmd.Start()
7574
if err != nil {
76-
log.Printf("Cannot spawn process: %s\n", err)
75+
slog.Error("Cannot spawn viewer process", "err", err)
7776
}
7877
defer handling.ReleaseProcess(cmd.Process)
7978

internal/commands/ssh.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package commands
22

33
import (
44
"fmt"
5+
"log/slog"
56
"os"
67
"syscall"
78

8-
log "github.com/sirupsen/logrus"
9-
109
"github.com/eikendev/hackenv/internal/constants"
1110
"github.com/eikendev/hackenv/internal/handling"
1211
"github.com/eikendev/hackenv/internal/images"
@@ -30,7 +29,7 @@ func (c *SSHCommand) Run(s *options.Options) error {
3029

3130
ipAddr, err := libvirt.GetDomainIPAddress(dom, &image)
3231
if err != nil {
33-
log.Errorf("Cannot retrieve guest's IP address\n")
32+
slog.Error("Cannot retrieve guest IP address", "err", err)
3433
return err
3534
}
3635

@@ -42,7 +41,7 @@ func (c *SSHCommand) Run(s *options.Options) error {
4241
//#nosec G204
4342
err = syscall.Exec(args[0], args, os.Environ())
4443
if err != nil {
45-
log.Printf("Cannot spawn process: %s\n", err)
44+
slog.Error("Cannot spawn SSH process", "err", err)
4645
}
4746

4847
return nil

internal/commands/status.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package commands
22

33
import (
44
"fmt"
5-
6-
log "github.com/sirupsen/logrus"
5+
"log/slog"
76

87
"github.com/eikendev/hackenv/internal/handling"
98
"github.com/eikendev/hackenv/internal/images"
@@ -31,7 +30,7 @@ func (*StatusCommand) Run(_ *options.Options) error {
3130

3231
info, err := dom.GetInfo()
3332
if err != nil {
34-
log.Printf("Cannot get domain info: %s\n", err)
33+
slog.Warn("Cannot get domain info", "err", err, "domain", image.Name)
3534
continue
3635
}
3736

0 commit comments

Comments
 (0)