Skip to content

Commit 200575b

Browse files
committed
Add prettier logging
1 parent 55dae3a commit 200575b

File tree

10 files changed

+114
-58
lines changed

10 files changed

+114
-58
lines changed

cmd/ctrlc/ctrlc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4-
"fmt"
54
"os"
65

6+
"github.com/charmbracelet/log"
77
"github.com/ctrlplanedev/cli/cmd/ctrlc/root"
88
"github.com/mitchellh/go-homedir"
99
"github.com/spf13/cobra"
@@ -40,7 +40,7 @@ func initConfig() {
4040
// Find home directory.
4141
home, err := homedir.Dir()
4242
if err != nil {
43-
fmt.Println(err)
43+
log.Error("Can't find home directory", "error", err)
4444
os.Exit(1)
4545
}
4646

@@ -51,7 +51,7 @@ func initConfig() {
5151
}
5252

5353
if err := viper.ReadInConfig(); err != nil {
54-
fmt.Println("Can't read config:", err)
54+
log.Error("Can't read config", "error", err)
5555
os.Exit(1)
5656
}
5757
}

cmd/ctrlc/root/agent/run/run.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package run
22

33
import (
4-
"log"
54
"strings"
65
"time"
76

7+
"github.com/charmbracelet/log"
88
"github.com/ctrlplanedev/cli/pkg/agent"
99
"github.com/spf13/cobra"
1010
"github.com/spf13/viper"
@@ -31,13 +31,13 @@ func NewAgentRunCmd() *cobra.Command {
3131
proxyAddr = "wss://" + proxyAddr
3232
}
3333

34-
log.Printf("Starting agent %q in workspace %q", agentName, workspace)
35-
log.Printf("Connecting to proxy at %s", proxyAddr)
34+
log.Info("Starting agent", "name", agentName, "workspace", workspace)
35+
log.Info("Connecting to proxy", "address", proxyAddr)
3636
if len(metadata) > 0 {
37-
log.Printf("With metadata: %v", metadata)
37+
log.Info("With metadata", "metadata", metadata)
3838
}
3939
if len(associatedResources) > 0 {
40-
log.Printf("Associated with resources: %v", associatedResources)
40+
log.Info("Associated with resources", "resources", associatedResources)
4141
}
4242

4343
apiKey := viper.GetString("api-key")
@@ -58,7 +58,7 @@ func NewAgentRunCmd() *cobra.Command {
5858
<-agent.StopSignal
5959
}
6060

61-
log.Printf("Failed to connect: %v. Retrying in %v...", err, backoff)
61+
log.Warn("Failed to connect", "error", err)
6262
time.Sleep(backoff)
6363
backoff *= 2
6464
if backoff > maxBackoff {

cmd/ctrlc/root/config/set/set.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/MakeNowJust/heredoc/v2"
7+
"github.com/charmbracelet/log"
78
"github.com/spf13/cobra"
89
"github.com/spf13/viper"
910
)
@@ -49,7 +50,7 @@ func NewSetCmd() *cobra.Command {
4950
return fmt.Errorf("failed to write config: %w", err)
5051
}
5152

52-
fmt.Printf("Successfully set %s = %s\n", key, value)
53+
log.Info("Successfully set", "key", key, "value", value)
5354
return nil
5455
},
5556
}

cmd/ctrlc/root/root.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package root
22

33
import (
44
"github.com/MakeNowJust/heredoc/v2"
5-
"github.com/spf13/cobra"
65

6+
"github.com/charmbracelet/log"
77
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/agent"
88
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api"
99
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/config"
10+
"github.com/spf13/cobra"
1011
)
1112

1213
func NewRootCmd() *cobra.Command {
14+
var logLevel string
15+
1316
cmd := &cobra.Command{
1417
Use: "ctrlc <command> <subcommand> [subcommand] [flags]",
1518
Short: "Ctrlconnect CLI",
@@ -18,11 +21,26 @@ func NewRootCmd() *cobra.Command {
1821
$ ctrlc agent run
1922
$ ctrlc connect <agent-name>
2023
`),
24+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
25+
switch logLevel {
26+
case "debug":
27+
log.SetLevel(log.DebugLevel)
28+
case "info":
29+
log.SetLevel(log.InfoLevel)
30+
case "warn":
31+
log.SetLevel(log.WarnLevel)
32+
case "error":
33+
log.SetLevel(log.ErrorLevel)
34+
}
35+
return nil
36+
},
2137
RunE: func(cmd *cobra.Command, args []string) error {
2238
return cmd.Help()
2339
},
2440
}
2541

42+
cmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Set the logging level (debug, info, warn, error)")
43+
2644
cmd.AddCommand(agent.NewAgentCmd())
2745
cmd.AddCommand(api.NewAPICmd())
2846
cmd.AddCommand(config.NewConfigCmd())

go.mod

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,22 @@ require (
1818
require (
1919
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
2020
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
21+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
22+
github.com/charmbracelet/lipgloss v0.10.0 // indirect
23+
github.com/charmbracelet/log v0.4.0 // indirect
2124
github.com/fsnotify/fsnotify v1.7.0 // indirect
25+
github.com/go-logfmt/logfmt v0.6.0 // indirect
2226
github.com/hashicorp/hcl v1.0.0 // indirect
2327
github.com/inconshreveable/mousetrap v1.1.0 // indirect
28+
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
2429
github.com/magiconair/properties v1.8.7 // indirect
30+
github.com/mattn/go-isatty v0.0.20 // indirect
31+
github.com/mattn/go-runewidth v0.0.15 // indirect
2532
github.com/mitchellh/mapstructure v1.5.0 // indirect
33+
github.com/muesli/reflow v0.3.0 // indirect
34+
github.com/muesli/termenv v0.15.2 // indirect
2635
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
36+
github.com/rivo/uniseg v0.4.7 // indirect
2737
github.com/sagikazarmark/locafero v0.4.0 // indirect
2838
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
2939
github.com/sourcegraph/conc v0.3.0 // indirect
@@ -33,7 +43,7 @@ require (
3343
github.com/subosito/gotenv v1.6.0 // indirect
3444
go.uber.org/atomic v1.9.0 // indirect
3545
go.uber.org/multierr v1.9.0 // indirect
36-
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
46+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
3747
golang.org/x/sys v0.18.0 // indirect
3848
golang.org/x/text v0.14.0 // indirect
3949
gopkg.in/ini.v1 v1.67.0 // indirect

go.sum

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ github.com/MakeNowJust/heredoc/v2 v2.0.1/go.mod h1:6/2Abh5s+hc3g9nbWLe9ObDIOhaRr
55
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
66
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
77
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
8+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
9+
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
810
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
11+
github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=
12+
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
13+
github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM=
14+
github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM=
915
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
1016
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
1117
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
@@ -17,6 +23,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
1723
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
1824
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
1925
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
26+
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
27+
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
2028
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
2129
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
2230
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
@@ -32,21 +40,36 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
3240
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
3341
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
3442
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
43+
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
44+
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
3545
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
3646
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
47+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
48+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
49+
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
50+
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
51+
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
3752
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
3853
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
3954
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
4055
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
4156
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
4257
github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
58+
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
59+
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
60+
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
61+
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
4362
github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro=
4463
github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg=
4564
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
4665
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
4766
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4867
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
4968
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
69+
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
70+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
71+
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
72+
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
5073
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
5174
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
5275
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -85,7 +108,10 @@ go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
85108
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
86109
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
87110
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
111+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
112+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
88113
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
114+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
89115
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
90116
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
91117
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=

internal/ptysession/manager.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package ptysession
22

33
import (
4-
"log"
54
"sync"
65
"time"
6+
7+
"github.com/charmbracelet/log"
78
)
89

910
// Manager handles concurrent access to PTY sessions
@@ -22,7 +23,7 @@ var (
2223
// initializes the manager on first call using sync.Once.
2324
func GetManager() *Manager {
2425
managerOnce.Do(func() {
25-
log.Printf("Initializing session manager")
26+
log.Info("Initializing session manager")
2627
manager = &Manager{
2728
sessions: make(map[string]*Session),
2829
}
@@ -33,11 +34,11 @@ func GetManager() *Manager {
3334
// AddSession adds a new PTY session to the manager. It acquires a write lock to
3435
// safely add the session to the map.
3536
func (m *Manager) AddSession(id string, session *Session) {
36-
log.Printf("Adding session %s to manager", id)
37+
log.Info("Adding session", "id", id)
3738
m.mu.Lock()
3839
defer m.mu.Unlock()
3940
m.sessions[id] = session
40-
log.Printf("Successfully added session %s", id)
41+
log.Info("Successfully added session", "id", id)
4142
}
4243

4344
// GetSession retrieves a session by its ID. It returns the session and a
@@ -48,53 +49,53 @@ func (m *Manager) GetSession(id string) (*Session, bool) {
4849
defer m.mu.RUnlock()
4950
session, exists := m.sessions[id]
5051
if !exists {
51-
log.Printf("Session %s not found", id)
52+
log.Warn("Session not found", "id", id)
5253
}
5354
return session, exists
5455
}
5556

5657
// RemoveSession removes a session from the manager. It acquires a write lock to
5758
// safely delete the session from the map.
5859
func (m *Manager) RemoveSession(id string) {
59-
log.Printf("Removing session %s from manager", id)
60+
log.Info("Removing session", "id", id)
6061
m.mu.Lock()
6162
defer m.mu.Unlock()
6263
delete(m.sessions, id)
63-
log.Printf("Successfully removed session %s", id)
64+
log.Info("Successfully removed session", "id", id)
6465
}
6566

6667
// ListSessions returns a list of active session IDs. It acquires a read lock
6768
// since it only reads from the sessions map. Returns a slice containing all
6869
// session IDs.
6970
func (m *Manager) ListSessions() []string {
70-
log.Printf("Listing all active sessions")
71+
log.Info("Listing all active sessions")
7172
m.mu.RLock()
7273
defer m.mu.RUnlock()
7374
ids := make([]string, 0, len(m.sessions))
7475
for id := range m.sessions {
7576
ids = append(ids, id)
7677
}
77-
log.Printf("Found %d active sessions", len(ids))
78+
log.Info("Found active sessions", "count", len(ids))
7879
return ids
7980
}
8081

8182
func (m *Manager) StartSessionCleaner(timeout time.Duration) {
8283
go func() {
83-
log.Printf("Starting session cleaner with timeout %v", timeout)
84+
log.Info("Starting session cleaner", "timeout", timeout)
8485
for {
8586
time.Sleep(timeout / 2)
86-
log.Printf("Running session cleanup check")
87+
log.Info("Running session cleanup check")
8788
m.mu.Lock()
8889
for id, session := range m.sessions {
8990
idleTime := time.Since(session.LastActivity)
9091
if idleTime > timeout {
91-
log.Printf("Cleaning up inactive session %s (idle for %v)", id, idleTime)
92+
log.Info("Cleaning up inactive session", "id", id, "idle", idleTime)
9293
session.CancelFunc()
9394
delete(m.sessions, id)
9495
}
9596
}
9697
m.mu.Unlock()
97-
log.Printf("Completed session cleanup check")
98+
log.Info("Completed session cleanup check")
9899
}
99100
}()
100101
}

0 commit comments

Comments
 (0)