Skip to content

Commit ca7cee2

Browse files
authored
feature: enable cmd timeout flag
1 parent 7653ea4 commit ca7cee2

49 files changed

Lines changed: 504 additions & 179 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/agent/config.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package agent
66

77
import (
88
"os"
9+
"time"
910

1011
"github.com/spf13/cobra"
1112
"github.com/tschaefer/finchctl/cmd/completion"
@@ -33,12 +34,18 @@ func runConfigCmd(cmd *cobra.Command, args []string) {
3334
formatType, err := format.GetRunFormat("quiet")
3435
cobra.CheckErr(err)
3536

37+
timeout, _ := cmd.Flags().GetUint("run.cmd-timeout")
38+
3639
rid, _ := cmd.Flags().GetString("agent.rid")
3740
if rid == "" {
3841
errors.CheckErr("agent resource identifier is required", formatType)
3942
}
4043

41-
agent, err := agent.New("", "local", formatType, false)
44+
agent, err := agent.New(cmd.Context(), agent.Options{
45+
TargetURL: "local",
46+
Format: formatType,
47+
CmdTimeout: time.Duration(timeout) * time.Second,
48+
})
4249
errors.CheckErr(err, formatType)
4350

4451
config, err := agent.Config(serviceName, rid)

cmd/agent/deploy.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package agent
66

77
import (
88
"fmt"
9+
"time"
910

1011
"github.com/spf13/cobra"
1112
"github.com/tschaefer/finchctl/cmd/completion"
@@ -43,7 +44,14 @@ func runDeployCmd(cmd *cobra.Command, args []string) {
4344

4445
targetUrl := args[0]
4546

46-
a, err := agent.New(config, targetUrl, formatType, dryRun)
47+
timeout, _ := cmd.Flags().GetUint("run.cmd-timeout")
48+
a, err := agent.New(cmd.Context(), agent.Options{
49+
Config: config,
50+
TargetURL: targetUrl,
51+
Format: formatType,
52+
DryRun: dryRun,
53+
CmdTimeout: time.Duration(timeout) * time.Second,
54+
})
4755
errors.CheckErr(err, formatType)
4856

4957
alloyVersion, _ := cmd.Flags().GetString("alloy.version")

cmd/agent/deregister.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Licensed under the MIT license, see LICENSE in the project root for details.
55
package agent
66

77
import (
8+
"time"
9+
810
"github.com/spf13/cobra"
911
"github.com/tschaefer/finchctl/cmd/completion"
1012
"github.com/tschaefer/finchctl/cmd/errors"
@@ -30,12 +32,18 @@ func runDeregisterCmd(cmd *cobra.Command, args []string) {
3032
formatType, err := format.GetRunFormat("quiet")
3133
cobra.CheckErr(err)
3234

35+
timeout, _ := cmd.Flags().GetUint("run.cmd-timeout")
36+
3337
rid, _ := cmd.Flags().GetString("agent.rid")
3438
if rid == "" {
3539
errors.CheckErr("agent resource identifier is required", formatType)
3640
}
3741

38-
agent, err := agent.New("", "local", formatType, false)
42+
agent, err := agent.New(cmd.Context(), agent.Options{
43+
TargetURL: "local",
44+
Format: formatType,
45+
CmdTimeout: time.Duration(timeout) * time.Second,
46+
})
3947
errors.CheckErr(err, formatType)
4048

4149
err = agent.Deregister(serviceName, rid)

cmd/agent/describe.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"strings"
1212

1313
"github.com/olekukonko/tablewriter"
14+
"time"
15+
1416
"github.com/spf13/cobra"
1517
"github.com/tschaefer/finchctl/cmd/completion"
1618
"github.com/tschaefer/finchctl/cmd/errors"
@@ -37,12 +39,18 @@ func runDescribeCmd(cmd *cobra.Command, args []string) {
3739
formatType, err := format.GetRunFormat("quiet")
3840
cobra.CheckErr(err)
3941

42+
timeout, _ := cmd.Flags().GetUint("run.cmd-timeout")
43+
4044
rid, _ := cmd.Flags().GetString("agent.rid")
4145
if rid == "" {
4246
errors.CheckErr("agent resource identifier is required", formatType)
4347
}
4448

45-
agent, err := agent.New("", "local", formatType, false)
49+
agent, err := agent.New(cmd.Context(), agent.Options{
50+
TargetURL: "local",
51+
Format: formatType,
52+
CmdTimeout: time.Duration(timeout) * time.Second,
53+
})
4654
errors.CheckErr(err, formatType)
4755

4856
desc, err := agent.Describe(serviceName, rid)

cmd/agent/edit.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package agent
66

77
import (
88
"fmt"
9+
"time"
910

1011
"github.com/spf13/cobra"
1112
"github.com/spf13/viper"
@@ -66,7 +67,12 @@ func runEditCmd(cmd *cobra.Command, args []string) {
6667
serviceName := args[0]
6768
data := editParseFlags(formatType)
6869

69-
a, err := agent.New("", "localhost", formatType, false)
70+
timeout, _ := cmd.Flags().GetUint("run.cmd-timeout")
71+
a, err := agent.New(cmd.Context(), agent.Options{
72+
TargetURL: "localhost",
73+
Format: formatType,
74+
CmdTimeout: time.Duration(timeout) * time.Second,
75+
})
7076
cobra.CheckErr(err)
7177

7278
err = a.Edit(serviceName, data)

cmd/agent/list.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"fmt"
1010
"os"
11+
"time"
1112

1213
"github.com/spf13/cobra"
1314
"github.com/tschaefer/finchctl/cmd/completion"
@@ -36,7 +37,12 @@ func runListCmd(cmd *cobra.Command, args []string) {
3637
formatType, err := format.GetRunFormat("quiet")
3738
cobra.CheckErr(err)
3839

39-
a, err := agent.New("", "localhost", formatType, false)
40+
timeout, _ := cmd.Flags().GetUint("run.cmd-timeout")
41+
a, err := agent.New(cmd.Context(), agent.Options{
42+
TargetURL: "localhost",
43+
Format: formatType,
44+
CmdTimeout: time.Duration(timeout) * time.Second,
45+
})
4046
errors.CheckErr(err, formatType)
4147

4248
list, err := a.List(serviceName)

cmd/agent/register.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package agent
77
import (
88
"fmt"
99
"os"
10+
"time"
1011

1112
"github.com/spf13/cobra"
1213
"github.com/spf13/viper"
@@ -76,7 +77,12 @@ func runRegisterCmd(cmd *cobra.Command, args []string) {
7677
serviceName := args[0]
7778
data := parseFlags(formatType)
7879

79-
a, err := agent.New("", "localhost", formatType, false)
80+
timeout, _ := cmd.Flags().GetUint("run.cmd-timeout")
81+
a, err := agent.New(cmd.Context(), agent.Options{
82+
TargetURL: "localhost",
83+
Format: formatType,
84+
CmdTimeout: time.Duration(timeout) * time.Second,
85+
})
8086
cobra.CheckErr(err)
8187

8288
config, err := a.Register(serviceName, data)

cmd/agent/teardown.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Licensed under the MIT license, see LICENSE in the project root for details.
55
package agent
66

77
import (
8+
"time"
9+
810
"github.com/spf13/cobra"
911
"github.com/tschaefer/finchctl/cmd/completion"
1012
"github.com/tschaefer/finchctl/cmd/errors"
@@ -35,7 +37,13 @@ func runTeardownCmd(cmd *cobra.Command, args []string) {
3537

3638
targetUrl := args[0]
3739

38-
a, err := agent.New("", targetUrl, formatType, dryRun)
40+
timeout, _ := cmd.Flags().GetUint("run.cmd-timeout")
41+
a, err := agent.New(cmd.Context(), agent.Options{
42+
TargetURL: targetUrl,
43+
Format: formatType,
44+
DryRun: dryRun,
45+
CmdTimeout: time.Duration(timeout) * time.Second,
46+
})
3947
errors.CheckErr(err, formatType)
4048

4149
err = a.Teardown()

cmd/agent/update.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package agent
66

77
import (
88
"fmt"
9+
"time"
910

1011
"github.com/spf13/cobra"
1112
"github.com/tschaefer/finchctl/cmd/completion"
@@ -53,7 +54,14 @@ func runUpdateCmd(cmd *cobra.Command, args []string) {
5354

5455
targetUrl := args[0]
5556

56-
a, err := agent.New(config, targetUrl, formatType, dryRun)
57+
timeout, _ := cmd.Flags().GetUint("run.cmd-timeout")
58+
a, err := agent.New(cmd.Context(), agent.Options{
59+
Config: config,
60+
TargetURL: targetUrl,
61+
Format: formatType,
62+
DryRun: dryRun,
63+
CmdTimeout: time.Duration(timeout) * time.Second,
64+
})
5765
errors.CheckErr(err, formatType)
5866

5967
err = a.Update(skipConfig, skipBinaries, alloyVersion)

cmd/errors/errors.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ func CheckErr(msg any, format target.Format) {
2424
"timestamp": time.Now().Format(time.RFC3339),
2525
"error": fmt.Sprintf("%v", msg),
2626
}
27-
jsonData, _ := json.Marshal(data)
27+
jsonData, err := json.Marshal(data)
28+
if err != nil {
29+
fmt.Fprintf(os.Stderr, "Failed to marshal error message: %v\n", err)
30+
os.Exit(1)
31+
}
2832
fmt.Println(string(jsonData))
2933
default:
3034
fmt.Println(msg)

0 commit comments

Comments
 (0)