Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: 🔵 Setup Go
uses: actions/setup-go@v6
with:
go-version: 1.25.x
go-version: 1.26.x

- uses: pre-commit/action@v3.0.1

Expand All @@ -49,7 +49,7 @@ jobs:
- name: 🔵 Setup Go
uses: actions/setup-go@v6
with:
go-version: 1.25.x
go-version: 1.26.x

- name: ⏬ Install Dependencies
run: go get .
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/golangci/golangci-lint
rev: v2.8.0
rev: v2.12.2
hooks:
- id: golangci-lint-fmt
2 changes: 1 addition & 1 deletion cmd/info/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package info

var Version = "0.0.54"
var Version = "0.0.55"
86 changes: 24 additions & 62 deletions cmd/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,99 +2,61 @@ package log

import (
"bytes"
"io"
"regexp"
"testing"

"github.com/kloudkit/ws-cli/internals/logger"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
)

func TestLogCommand(t *testing.T) {
t.Run("WarnInvokesLogWithFlags", func(t *testing.T) {
var gotLevel, gotMsg string
var gotIndent int
var gotStamp bool
called := 0
func stripAnsi(s string) string {
re := regexp.MustCompile(`\x1b\[[0-9;]*m`)

original := logger.Log
logger.Log = func(w io.Writer, level, message string, indent int, withStamp bool) {
called++
gotLevel = level
gotMsg = message
gotIndent = indent
gotStamp = withStamp
}
defer func() { logger.Log = original }()
return re.ReplaceAllString(s, "")
}

func TestLogCommand(t *testing.T) {
t.Run("WarnInvokesLogWithFlags", func(t *testing.T) {
buffer := new(bytes.Buffer)
cmd := LogCmd
cmd.SetOut(buffer)
cmd.SetArgs([]string{"warn", "hello", "--indent", "2", "--stamp"})

err := cmd.Execute()
assert.NilError(t, err)
assert.Equal(t, 1, called)
assert.Equal(t, "warn", gotLevel)
assert.Equal(t, "hello", gotMsg)
assert.Equal(t, 2, gotIndent)
assert.Assert(t, gotStamp)
assert.Assert(t, cmp.Regexp(
`^\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z] warn - hello\n$`,
stripAnsi(buffer.String()),
))
})

t.Run("InfoUsesPipeWhenFlagged", func(t *testing.T) {
var gotLevel string
var gotIndent int
var gotStamp bool
called := 0

original := logger.Pipe
logger.Pipe = func(r io.Reader, w io.Writer, level string, indent int, withStamp bool) {
called++
gotLevel = level
gotIndent = indent
gotStamp = withStamp
}
defer func() { logger.Pipe = original }()

buffer := new(bytes.Buffer)
cmd := LogCmd
cmd.SetIn(bytes.NewBufferString("foo\n"))
cmd.SetOut(new(bytes.Buffer))
cmd.SetOut(buffer)
cmd.SetArgs([]string{"info", "--pipe", "--indent", "1", "--stamp"})

err := cmd.Execute()
assert.NilError(t, err)
assert.Equal(t, 1, called)
assert.Equal(t, "info", gotLevel)
assert.Equal(t, 1, gotIndent)
assert.Assert(t, gotStamp)
assert.Assert(t, cmp.Regexp(
`^\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z] info - foo\n$`,
stripAnsi(buffer.String()),
))
})

t.Run("StampInvokesLog", func(t *testing.T) {
called := 0
var gotLevel, gotMsg string
var gotIndent int
var gotStamp bool

original := logger.Log
logger.Log = func(w io.Writer, level, message string, indent int, withStamp bool) {
called++
gotLevel = level
gotMsg = message
gotIndent = indent
gotStamp = withStamp
}
defer func() { logger.Log = original }()

buffer := new(bytes.Buffer)
cmd := LogCmd
cmd.PersistentFlags().Set("pipe", "false")
cmd.SetOut(new(bytes.Buffer))
cmd.SetOut(buffer)
cmd.SetArgs([]string{"stamp"})

err := cmd.Execute()
assert.NilError(t, err)
assert.Equal(t, 1, called)
assert.Equal(t, "", gotLevel)
assert.Equal(t, "", gotMsg)
assert.Equal(t, 0, gotIndent)
assert.Assert(t, gotStamp)
assert.Assert(t, cmp.Regexp(
`^\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z] \n$`,
stripAnsi(buffer.String()),
))
})
}
65 changes: 23 additions & 42 deletions cmd/show/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,34 @@ var ipCmd = &cobra.Command{
Short: "Display IP addresses",
}

var ipInternalCmd = &cobra.Command{
Use: "internal",
Short: "Display the internal IP address",
RunE: func(cmd *cobra.Command, args []string) error {
ip, err := net.GetInternalIP()
if err != nil {
return err
}

raw, _ := cmd.Flags().GetBool("raw")
if styles.OutputRaw(cmd.OutOrStdout(), raw, ip) {
return nil
}

styles.PrintTitle(cmd.OutOrStdout(), "Internal IP Address")
styles.PrintKeyCode(cmd.OutOrStdout(), "Address", ip)

return nil
},
}
func makeIPCmd(use, short, title string, getter func() (string, error)) *cobra.Command {
return &cobra.Command{
Use: use,
Short: short,
RunE: func(cmd *cobra.Command, args []string) error {
ip, err := getter()
if err != nil {
return err
}

raw, _ := cmd.Flags().GetBool("raw")
if styles.OutputRaw(cmd.OutOrStdout(), raw, ip) {
return nil
}

styles.PrintTitle(cmd.OutOrStdout(), title)
styles.PrintKeyCode(cmd.OutOrStdout(), "Address", ip)

var ipNodeCmd = &cobra.Command{
Use: "node",
Short: "Display the node/host IP address",
RunE: func(cmd *cobra.Command, args []string) error {
ip, err := net.GetNodeIP()
if err != nil {
return err
}

raw, _ := cmd.Flags().GetBool("raw")
if styles.OutputRaw(cmd.OutOrStdout(), raw, ip) {
return nil
}

styles.PrintTitle(cmd.OutOrStdout(), "Node IP Address")
styles.PrintKeyCode(cmd.OutOrStdout(), "Address", ip)

return nil
},
},
}
}

func init() {
ipInternalCmd.Flags().Bool("raw", false, "Output raw value without styling")
ipNodeCmd.Flags().Bool("raw", false, "Output raw value without styling")

ipCmd.AddCommand(ipInternalCmd, ipNodeCmd)
ipCmd.AddCommand(
makeIPCmd("internal", "Display the internal IP address", "Internal IP Address", net.GetInternalIP),
makeIPCmd("node", "Display the node/host IP address", "Node IP Address", net.GetNodeIP),
)

ShowCmd.AddCommand(ipCmd)
}
50 changes: 50 additions & 0 deletions cmd/show/ip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package show

import (
"bytes"
"errors"
"strings"
"testing"

"gotest.tools/v3/assert"
)

func _runIPCmd(t *testing.T, title string, getter func() (string, error)) (stdout, stderr string, err error) {
t.Helper()
cmd := makeIPCmd("ip", "Display an IP address", title, getter)

var outBuf, errBuf bytes.Buffer
cmd.SetOut(&outBuf)
cmd.SetErr(&errBuf)
cmd.SetArgs([]string{})
err = cmd.Execute()
return outBuf.String(), errBuf.String(), err
}

func TestShowIP_Internal_RendersValue(t *testing.T) {
getter := func() (string, error) { return "10.0.0.1", nil }

stdout, _, err := _runIPCmd(t, "Internal IP Address", getter)
assert.NilError(t, err)
plain := _stripANSI(stdout)
assert.Assert(t, strings.Contains(strings.ToUpper(plain), "INTERNAL IP ADDRESS"), "want title, got: %q", plain)
assert.Assert(t, strings.Contains(plain, "10.0.0.1"), "want IP value, got: %q", plain)
}

func TestShowIP_Node_RendersValue(t *testing.T) {
getter := func() (string, error) { return "192.168.1.5", nil }

stdout, _, err := _runIPCmd(t, "Node IP Address", getter)
assert.NilError(t, err)
plain := _stripANSI(stdout)
assert.Assert(t, strings.Contains(strings.ToUpper(plain), "NODE IP ADDRESS"), "want title, got: %q", plain)
assert.Assert(t, strings.Contains(plain, "192.168.1.5"), "want IP value, got: %q", plain)
}

func TestShowIP_GetterError_NonZeroExit(t *testing.T) {
getter := func() (string, error) { return "", errors.New("simulated") }

_, _, err := _runIPCmd(t, "Internal IP Address", getter)
assert.Assert(t, err != nil, "want non-nil error (maps to non-zero exit at root)")
assert.ErrorContains(t, err, "simulated")
}
2 changes: 0 additions & 2 deletions cmd/show/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ var pathVscodeCmd = &cobra.Command{
}

func init() {
pathHomeCmd.Flags().Bool("raw", false, "Output raw value without styling")
pathVscodeCmd.Flags().Bool("raw", false, "Output raw value without styling")
pathVscodeCmd.Flags().Bool("workspace", false, "Get the workspace settings")

pathCmd.AddCommand(pathHomeCmd, pathVscodeCmd)
Expand Down
4 changes: 4 additions & 0 deletions cmd/show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ var ShowCmd = &cobra.Command{
Use: "show",
Short: "Display information about the current workspace instance",
}

func init() {
ShowCmd.PersistentFlags().Bool("raw", false, "Output raw value without styling")
}
22 changes: 22 additions & 0 deletions cmd/show/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ func _runShow(t *testing.T, args ...string) (stdout, stderr string, exit int) {
_ = f.Value.Set(f.DefValue)
})

ShowCmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
f.Changed = false
_ = f.Value.Set(f.DefValue)
})

var outBuf, errBuf bytes.Buffer
ShowCmd.SetOut(&outBuf)
ShowCmd.SetErr(&errBuf)
Expand Down Expand Up @@ -490,6 +495,23 @@ func TestShowEnv_NonSecret_FilePrefix_ErrorPath(t *testing.T) {
assert.Assert(t, strings.Contains(stderr, "WS_SERVER_ROOT"), "want runtimeKey in stderr, got: %q", stderr)
}

func TestShow_RawFlagInheritedByAllLeaves(t *testing.T) {
cases := []struct{ args []string }{
{[]string{"path", "home", "--raw"}},
{[]string{"path", "vscode-settings", "--raw"}},
{[]string{"ip", "internal", "--raw"}},
{[]string{"ip", "node", "--raw"}},
{[]string{"env", "WS_SERVER_ROOT", "--raw"}},
}

for _, c := range cases {
_installEnvFixture(t)
_, stderr, exit := _runShow(t, c.args...)
assert.Equal(t, 0, exit, "args=%v stderr=%q", c.args, stderr)
assert.Assert(t, !strings.Contains(stderr, "unknown flag"), "args=%v stderr=%q", c.args, stderr)
}
}

func TestShowEnv_CheckUnchanged(t *testing.T) {
_installEnvFixture(t)
t.Setenv("WS_SERVER_PORT", "")
Expand Down
28 changes: 14 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
module github.com/kloudkit/ws-cli

go 1.25.8
go 1.26.2

require (
charm.land/fang/v2 v2.0.1
charm.land/glamour/v2 v2.0.0
charm.land/lipgloss/v2 v2.0.3
github.com/prometheus/client_golang v1.23.2
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
golang.org/x/crypto v0.50.0
golang.org/x/term v0.42.0
golang.org/x/crypto v0.51.0
golang.org/x/term v0.43.0
gopkg.in/yaml.v3 v3.0.1
gotest.tools/v3 v3.5.2
)

require (
charm.land/glamour/v2 v2.0.0 // indirect
github.com/alecthomas/chroma/v2 v2.14.0 // indirect
github.com/alecthomas/chroma/v2 v2.24.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/charmbracelet/colorprofile v0.4.3 // indirect
github.com/charmbracelet/ultraviolet v0.0.0-20260422141423-a0f1f21775f7 // indirect
github.com/charmbracelet/ultraviolet v0.0.0-20260511121909-c840852527f3 // indirect
github.com/charmbracelet/x/ansi v0.11.7 // indirect
github.com/charmbracelet/x/exp/charmtone v0.0.0-20260426004601-d5e63ff0b9ca // indirect
github.com/charmbracelet/x/exp/slice v0.0.0-20250327172914-2fdc97757edf // indirect
github.com/charmbracelet/x/exp/charmtone v0.0.0-20260517005351-920740d613be // indirect
github.com/charmbracelet/x/exp/slice v0.0.0-20260517005351-920740d613be // indirect
github.com/charmbracelet/x/term v0.2.2 // indirect
github.com/charmbracelet/x/termios v0.1.1 // indirect
github.com/charmbracelet/x/windows v0.2.2 // indirect
github.com/clipperhouse/displaywidth v0.11.0 // indirect
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
github.com/dlclark/regexp2 v1.11.0 // indirect
github.com/dlclark/regexp2 v1.12.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand All @@ -49,12 +49,12 @@ require (
github.com/prometheus/procfs v0.20.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/yuin/goldmark v1.7.8 // indirect
github.com/yuin/goldmark-emoji v1.0.5 // indirect
github.com/yuin/goldmark v1.8.2 // indirect
github.com/yuin/goldmark-emoji v1.0.6 // indirect
go.yaml.in/yaml/v2 v2.4.4 // indirect
golang.org/x/net v0.52.0 // indirect
golang.org/x/net v0.54.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.43.0 // indirect
golang.org/x/text v0.36.0 // indirect
golang.org/x/sys v0.44.0 // indirect
golang.org/x/text v0.37.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect
)
Loading
Loading