Skip to content

Commit 0d161fb

Browse files
marstrCopilot
andauthored
Add cmd.RootContext() (#110)
* Add cmd.RootContext function * Adopt cmd.RootContext in balance command. * Adopt cmd.RootContext in branch command * Adopt cmd.RootContext in bring-to command * Adopt cmd.RootContext in checkout command * Adopt cmd.RootContext in commit command. * Adopt cmd.RootContext in credit command. * Adopt cmd.RootContext in debit command. * Adopt cmd.RootContext in diff command. * Adopt cmd.RootContext in init command. * Adopt cmd.RootContext in log command. * Adopt cmd.RootContext in rev-parse command. * Adopt cmd.RootContext in revert command. * Adopt cmd.RootContext in show command. * Adopt cmd.RootContext in transfer command. * Actually, add real cancellation by default, even when using default background context. Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent ccadc98 commit 0d161fb

File tree

15 files changed

+67
-270
lines changed

15 files changed

+67
-270
lines changed

cmd/balance.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"path"
2424
"path/filepath"
2525
"sort"
26-
"time"
2726

2827
"github.com/marstr/envelopes"
2928
"github.com/sirupsen/logrus"
@@ -44,22 +43,8 @@ var balanceCmd = &cobra.Command{
4443
Aliases: []string{"bal", "b"},
4544
Short: "Scours a baronial directory (or subdirectory) for balance information.",
4645
Run: func(cmd *cobra.Command, args []string) {
47-
var timeout time.Duration
48-
var err error
49-
timeout, err = cmd.Flags().GetDuration(timeoutFlag)
50-
if err != nil {
51-
logrus.Fatal(err)
52-
}
53-
54-
var ctx context.Context
55-
if timeout > 0 {
56-
var cancel context.CancelFunc
57-
ctx, cancel = context.WithTimeout(context.Background(), timeout)
58-
defer cancel()
59-
60-
} else {
61-
ctx = context.Background()
62-
}
46+
ctx, cancel := RootContext(cmd)
47+
defer cancel()
6348

6449
var targetDir string
6550
if len(args) > 0 {
@@ -68,6 +53,7 @@ var balanceCmd = &cobra.Command{
6853
targetDir = "."
6954
}
7055

56+
var err error
7157
targetDir, err = filepath.Abs(targetDir)
7258
if err != nil {
7359
logrus.Fatal(err)

cmd/branch.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"io"
2424
"os"
2525
"path"
26-
"time"
2726

2827
"github.com/marstr/envelopes"
2928
"github.com/marstr/envelopes/persist"
@@ -40,22 +39,10 @@ var branchCmd = &cobra.Command{
4039
Short: "Creates a branch with a given name.",
4140
Args: cobra.MaximumNArgs(1),
4241
Run: func(cmd *cobra.Command, args []string) {
43-
var timeout time.Duration
44-
var err error
45-
timeout, err = cmd.Flags().GetDuration(timeoutFlag)
46-
if err != nil {
47-
logrus.Fatal(err)
48-
}
49-
50-
var ctx context.Context
51-
if timeout > 0 {
52-
var cancel context.CancelFunc
53-
ctx, cancel = context.WithTimeout(context.Background(), timeout)
54-
defer cancel()
55-
} else {
56-
ctx = context.Background()
57-
}
42+
ctx, cancel := RootContext(cmd)
43+
defer cancel()
5844

45+
var err error
5946
var indexRootDir string
6047
indexRootDir, err = index.RootDirectory(".")
6148
if err != nil {

cmd/bring-to.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
5-
"time"
6-
74
"github.com/marstr/baronial/internal/index"
85
"github.com/marstr/envelopes"
96
"github.com/sirupsen/logrus"
@@ -31,21 +28,8 @@ func init() {
3128
}
3229

3330
func RunBringTo(cmd *cobra.Command, args []string) error {
34-
var timeout time.Duration
35-
var err error
36-
timeout, err = cmd.Flags().GetDuration(timeoutFlag)
37-
if err != nil {
38-
logrus.Fatal(err)
39-
}
40-
41-
var ctx context.Context
42-
if timeout > 0 {
43-
var cancel context.CancelFunc
44-
ctx, cancel = context.WithTimeout(context.Background(), timeout)
45-
defer cancel()
46-
} else {
47-
ctx = context.Background()
48-
}
31+
ctx, cancel := RootContext(cmd)
32+
defer cancel()
4933

5034
desiredBal, err := envelopes.ParseBalance([]byte(args[0]))
5135
if err != nil {

cmd/checkout.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
package cmd
1717

1818
import (
19-
"context"
2019
"path"
21-
"time"
2220

2321
"github.com/marstr/envelopes"
2422
"github.com/marstr/envelopes/persist"
@@ -35,24 +33,11 @@ var checkoutCmd = &cobra.Command{
3533
Short: "Resets the index to show the balances at a particular transaction.",
3634
Args: cobra.ExactArgs(1),
3735
Run: func(cmd *cobra.Command, args []string) {
38-
var timeout time.Duration
39-
var err error
40-
timeout, err = cmd.Flags().GetDuration(timeoutFlag)
41-
if err != nil {
42-
logrus.Fatal(err)
43-
}
44-
45-
var ctx context.Context
46-
if timeout > 0 {
47-
var cancel context.CancelFunc
48-
ctx, cancel = context.WithTimeout(context.Background(), timeout)
49-
defer cancel()
50-
51-
} else {
52-
ctx = context.Background()
53-
}
36+
ctx, cancel := RootContext(cmd)
37+
defer cancel()
5438

5539
var root string
40+
var err error
5641
root, err = index.RootDirectory(".")
5742
if err != nil {
5843
logrus.Fatal(err)

cmd/commit.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,22 +138,8 @@ var commitCmd = &cobra.Command{
138138
return cobra.NoArgs(cmd, args)
139139
},
140140
Run: func(cmd *cobra.Command, _ []string) {
141-
var timeout time.Duration
142-
var err error
143-
timeout, err = cmd.Flags().GetDuration(timeoutFlag)
144-
if err != nil {
145-
logrus.Fatal(err)
146-
}
147-
148-
var ctx context.Context
149-
if timeout > 0 {
150-
var cancel context.CancelFunc
151-
ctx, cancel = context.WithTimeout(context.Background(), timeout)
152-
defer cancel()
153-
154-
} else {
155-
ctx = context.Background()
156-
}
141+
ctx, cancel := RootContext(cmd)
142+
defer cancel()
157143

158144
targetDir, err := index.RootDirectory(".")
159145
if err != nil {

cmd/credit.go

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
package cmd
1717

1818
import (
19-
"context"
2019
"fmt"
21-
"time"
2220

2321
"github.com/marstr/envelopes"
2422
"github.com/sirupsen/logrus"
@@ -33,22 +31,8 @@ var creditCmd = &cobra.Command{
3331
Short: "Makes funds available for one or more category of spending.",
3432
Args: creditDebitArgValidation,
3533
Run: func(cmd *cobra.Command, args []string) {
36-
var timeout time.Duration
37-
var err error
38-
timeout, err = cmd.Flags().GetDuration(timeoutFlag)
39-
if err != nil {
40-
logrus.Fatal(err)
41-
}
42-
43-
var ctx context.Context
44-
if timeout > 0 {
45-
var cancel context.CancelFunc
46-
ctx, cancel = context.WithTimeout(context.Background(), timeout)
47-
defer cancel()
48-
49-
} else {
50-
ctx = context.Background()
51-
}
34+
ctx, cancel := RootContext(cmd)
35+
defer cancel()
5236

5337
rawMagnitude := args[0]
5438
magnitude, err := envelopes.ParseBalance([]byte(rawMagnitude))
@@ -76,28 +60,14 @@ func init() {
7660
}
7761

7862
func creditDebitArgValidation(cmd *cobra.Command, args []string) error {
79-
var timeout time.Duration
80-
var err error
81-
timeout, err = cmd.Flags().GetDuration(timeoutFlag)
82-
if err != nil {
83-
logrus.Fatal(err)
84-
}
85-
86-
var ctx context.Context
87-
if timeout > 0 {
88-
var cancel context.CancelFunc
89-
ctx, cancel = context.WithTimeout(context.Background(), timeout)
90-
defer cancel()
91-
92-
} else {
93-
ctx = context.Background()
94-
}
63+
ctx, cancel := RootContext(cmd)
64+
defer cancel()
9565

9666
if argCount := len(args); argCount < 2 {
9767
return fmt.Errorf("too few arguments (%d). %q requires at least a balance and one budget or account", argCount, cmd.Name())
9868
}
9969

100-
_, err = envelopes.ParseBalance([]byte(args[0]))
70+
_, err := envelopes.ParseBalance([]byte(args[0]))
10171
if err != nil {
10272
return fmt.Errorf("%q not recognized as an amount", args[0])
10373
}

cmd/debit.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
package cmd
1717

1818
import (
19-
"context"
20-
"time"
21-
2219
"github.com/marstr/envelopes"
2320
"github.com/sirupsen/logrus"
2421
"github.com/spf13/cobra"
@@ -32,22 +29,8 @@ var debitCmd = &cobra.Command{
3229
Short: `Removes funds from a category of spending.`,
3330
Args: creditDebitArgValidation,
3431
Run: func(cmd *cobra.Command, args []string) {
35-
var timeout time.Duration
36-
var err error
37-
timeout, err = cmd.Flags().GetDuration(timeoutFlag)
38-
if err != nil {
39-
logrus.Fatal(err)
40-
}
41-
42-
var ctx context.Context
43-
if timeout > 0 {
44-
var cancel context.CancelFunc
45-
ctx, cancel = context.WithTimeout(context.Background(), timeout)
46-
defer cancel()
47-
48-
} else {
49-
ctx = context.Background()
50-
}
32+
ctx, cancel := RootContext(cmd)
33+
defer cancel()
5134

5235
rawMagnitude := args[0]
5336
magnitude, err := envelopes.ParseBalance([]byte(rawMagnitude))

cmd/diff.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"context"
2222
"errors"
2323
"path"
24-
"time"
2524

2625
"github.com/marstr/envelopes"
2726
"github.com/marstr/envelopes/persist"
@@ -40,25 +39,10 @@ var diffCmd = &cobra.Command{
4039
Args: cobra.MaximumNArgs(2),
4140
PreRunE: setPagedCobraOutput,
4241
Run: func(cmd *cobra.Command, args []string) {
43-
var timeout time.Duration
44-
var err error
45-
timeout, err = cmd.Flags().GetDuration(timeoutFlag)
46-
if err != nil {
47-
logrus.Fatal(err)
48-
}
49-
50-
var ctx context.Context
51-
if timeout > 0 {
52-
var cancel context.CancelFunc
53-
ctx, cancel = context.WithTimeout(context.Background(), timeout)
54-
defer cancel()
55-
56-
} else {
57-
ctx = context.Background()
58-
}
42+
ctx, cancel := RootContext(cmd)
43+
defer cancel()
5944

60-
var repoRoot string
61-
repoRoot, err = index.RootDirectory(".")
45+
repoRoot, err := index.RootDirectory(".")
6246
if err != nil {
6347
return
6448
}

cmd/init.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
package cmd
1717

1818
import (
19-
"context"
2019
"os"
21-
"time"
2220

2321
"github.com/marstr/envelopes"
2422
"github.com/marstr/envelopes/persist"
@@ -34,22 +32,8 @@ var initCmd = &cobra.Command{
3432
Short: "Creates a new Baronial repository in the current working directory.",
3533
Args: cobra.NoArgs,
3634
Run: func(cmd *cobra.Command, args []string) {
37-
var timeout time.Duration
38-
var err error
39-
timeout, err = cmd.Flags().GetDuration(timeoutFlag)
40-
if err != nil {
41-
logrus.Fatal(err)
42-
}
43-
44-
var ctx context.Context
45-
if timeout > 0 {
46-
var cancel context.CancelFunc
47-
ctx, cancel = context.WithTimeout(context.Background(), timeout)
48-
defer cancel()
49-
50-
} else {
51-
ctx = context.Background()
52-
}
35+
ctx, cancel := RootContext(cmd)
36+
defer cancel()
5337

5438
const initCmdFailurePrefix = "unable to initialize repository: "
5539

@@ -71,8 +55,7 @@ var initCmd = &cobra.Command{
7155
}
7256
}
7357

74-
var repo persist.RepositoryReaderWriter
75-
repo, err = filesystem.OpenRepositoryWithCache(ctx, index.RepoName, 10000)
58+
repo, err := filesystem.OpenRepositoryWithCache(ctx, index.RepoName, 10000)
7659
if err != nil {
7760
logrus.Fatal(err)
7861
}

cmd/log.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"os"
2323
"path/filepath"
2424
"strings"
25-
"time"
2625

2726
"github.com/marstr/envelopes"
2827
"github.com/marstr/envelopes/persist"
@@ -43,25 +42,10 @@ var logCmd = &cobra.Command{
4342
},
4443
PreRunE: setPagedCobraOutput,
4544
Run: func(cmd *cobra.Command, args []string) {
46-
var timeout time.Duration
47-
var err error
48-
timeout, err = cmd.Flags().GetDuration(timeoutFlag)
49-
if err != nil {
50-
logrus.Fatal(err)
51-
}
52-
53-
var ctx context.Context
54-
if timeout > 0 {
55-
var cancel context.CancelFunc
56-
ctx, cancel = context.WithTimeout(context.Background(), timeout)
57-
defer cancel()
58-
59-
} else {
60-
ctx = context.Background()
61-
}
45+
ctx, cancel := RootContext(cmd)
46+
defer cancel()
6247

63-
var root string
64-
root, err = index.RootDirectory(".")
48+
root, err := index.RootDirectory(".")
6549
if err != nil {
6650
logrus.Error(err)
6751
return

0 commit comments

Comments
 (0)