Skip to content

Commit 976ef50

Browse files
authored
feat: context for all api related calls (#387)
1 parent af1f4e8 commit 976ef50

File tree

105 files changed

+1188
-1086
lines changed

Some content is hidden

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

105 files changed

+1188
-1086
lines changed

cli/cobra.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package cli
22

33
import (
4+
"context"
45
"crypto/tls"
56
"fmt"
67
"io"
78
"os"
9+
"os/signal"
810
"regexp"
11+
"syscall"
912

1013
"github.com/Telmate/proxmox-api-go/proxmox"
1114
"github.com/spf13/cobra"
@@ -25,6 +28,24 @@ func init() {
2528
RootCmd.PersistentFlags().StringP("proxyurl", "p", "", "proxy url to connect to")
2629
}
2730

31+
func Context() context.Context {
32+
ctx, cancel := context.WithCancel(context.Background())
33+
34+
// Channel to catch OS signals
35+
signalChan := make(chan os.Signal, 1)
36+
37+
// Notify signalChan when SIGINT or SIGTERM is received
38+
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
39+
40+
// Goroutine to handle signal
41+
go func() {
42+
defer signal.Stop(signalChan) // Cleanup when done
43+
<-signalChan // Wait for a signal
44+
cancel() // Cancel the context
45+
}()
46+
return ctx
47+
}
48+
2849
func Execute() (err error) {
2950
if err = RootCmd.Execute(); err != nil {
3051
return
@@ -33,12 +54,12 @@ func Execute() (err error) {
3354
}
3455

3556
func NewClient() (c *proxmox.Client) {
36-
c, err := Client("", "", "", "", "")
57+
c, err := Client(Context(), "", "", "", "", "")
3758
LogFatalError(err)
3859
return
3960
}
4061

41-
func Client(apiUrl, userID, password, otp string, http_headers string) (c *proxmox.Client, err error) {
62+
func Client(ctx context.Context, apiUrl, userID, password, otp string, http_headers string) (c *proxmox.Client, err error) {
4263
insecure, _ := RootCmd.Flags().GetBool("insecure")
4364
timeout, _ := RootCmd.Flags().GetInt("timeout")
4465
proxyUrl, _ := RootCmd.Flags().GetString("proxyurl")
@@ -67,12 +88,12 @@ func Client(apiUrl, userID, password, otp string, http_headers string) (c *proxm
6788
if userRequiresAPIToken(userID) {
6889
c.SetAPIToken(userID, password)
6990
// As test, get the version of the server
70-
_, err = c.GetVersion()
91+
_, err = c.GetVersion(Context())
7192
if err != nil {
7293
err = fmt.Errorf("login error: %s", err)
7394
}
7495
} else {
75-
err = c.Login(userID, password, otp)
96+
err = c.Login(ctx, userID, password, otp)
7697
}
7798
return
7899
}

cli/command/content/iso/download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var iso_downloadCmd = &cobra.Command{
2424
if err != nil {
2525
return
2626
}
27-
err = proxmox.DownloadIsoFromUrl(c, config)
27+
err = proxmox.DownloadIsoFromUrl(cli.Context(), c, config)
2828
if err != nil {
2929
return
3030
}

cli/command/content/template/download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var template_downloadCmd = &cobra.Command{
2121
if err != nil {
2222
return
2323
}
24-
err = proxmox.DownloadLxcTemplate(c, config)
24+
err = proxmox.DownloadLxcTemplate(cli.Context(), c, config)
2525
if err != nil {
2626
return
2727
}

cli/command/content/template/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var template_listCmd = &cobra.Command{
1111
Short: "Prints a list of all LXC templates available for download in raw json format",
1212
Args: cobra.ExactArgs(1),
1313
RunE: func(cmd *cobra.Command, args []string) (err error) {
14-
templates, err := proxmox.ListTemplates(cli.NewClient(), args[0])
14+
templates, err := proxmox.ListTemplates(cli.Context(), cli.NewClient(), args[0])
1515
if err != nil {
1616
return
1717
}

cli/command/create/create-acmeaccount.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ For config examples see "example acmeaccount"`,
2020
return
2121
}
2222
c := cli.NewClient()
23-
err = config.CreateAcmeAccount(id, c)
23+
err = config.CreateAcmeAccount(cli.Context(), id, c)
2424
if err != nil {
2525
return
2626
}

cli/command/create/create-pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var create_poolCmd = &cobra.Command{
2020
err = proxmox.ConfigPool{
2121
Name: proxmox.PoolName(id),
2222
Comment: comment,
23-
}.Create(c)
23+
}.Create(cli.Context(), c)
2424
if err != nil {
2525
return
2626
}

cli/command/create/create-snapshot.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ var (
2525
memory = false
2626
client := cli.NewClient()
2727
vmr := proxmox.NewVmRef(id)
28-
_, err = client.GetVmInfo(vmr)
28+
_, err = client.GetVmInfo(cli.Context(), vmr)
2929
if err != nil {
3030
return
3131
}
32-
err = config.Create(client, vmr)
32+
err = config.Create(cli.Context(), client, vmr)
3333
if err != nil {
3434
return
3535
}

cli/command/create/create-storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ For config examples see "example storage"`,
2020
return
2121
}
2222
c := cli.NewClient()
23-
err = config.CreateWithValidate(id, c)
23+
err = config.CreateWithValidate(cli.Context(), id, c)
2424
if err != nil {
2525
return
2626
}

cli/command/create/guest/create-guest-lxc.go

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

33
import (
4+
"github.com/Telmate/proxmox-api-go/cli"
45
"github.com/spf13/cobra"
56
)
67

@@ -12,7 +13,7 @@ The config can be set with the --file flag or piped from stdin.
1213
For config examples see "example guest lxc"`,
1314
Args: cobra.ExactArgs(2),
1415
RunE: func(cmd *cobra.Command, args []string) (err error) {
15-
return createGuest(args, "LxcGuest")
16+
return createGuest(cli.Context(), args, "LxcGuest")
1617
},
1718
}
1819

cli/command/create/guest/create-guest-qemu.go

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

33
import (
4+
"github.com/Telmate/proxmox-api-go/cli"
45
"github.com/spf13/cobra"
56
)
67

@@ -12,7 +13,7 @@ var guest_qemuCmd = &cobra.Command{
1213
For config examples see "example guest qemu"`,
1314
Args: cobra.ExactArgs(2),
1415
RunE: func(cmd *cobra.Command, args []string) (err error) {
15-
return createGuest(args, "QemuGuest")
16+
return createGuest(cli.Context(), args, "QemuGuest")
1617
},
1718
}
1819

0 commit comments

Comments
 (0)