zrok/cmd/zrok/release.go

70 lines
1.5 KiB
Go
Raw Normal View History

2022-11-30 19:45:57 +01:00
package main
import (
httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/rest_client_zrok/share"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/tui"
2022-11-30 19:45:57 +01:00
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(newReleaseCommand().cmd)
}
type releaseCommand struct {
cmd *cobra.Command
}
func newReleaseCommand() *releaseCommand {
cmd := &cobra.Command{
2023-01-04 20:42:58 +01:00
Use: "release <shareToken>",
Short: "Release a reserved share",
2022-11-30 19:45:57 +01:00
Args: cobra.ExactArgs(1),
}
command := &releaseCommand{cmd: cmd}
cmd.Run = command.run
return command
}
func (cmd *releaseCommand) run(_ *cobra.Command, args []string) {
2023-01-04 20:42:58 +01:00
shrToken := args[0]
env, err := environment.LoadRoot()
2022-11-30 19:45:57 +01:00
if err != nil {
if !panicInstead {
2023-07-10 22:41:16 +02:00
tui.Error("unable to load environment", err)
2022-11-30 19:45:57 +01:00
}
panic(err)
}
if !env.IsEnabled() {
2023-01-09 23:48:19 +01:00
tui.Error("unable to load environment; did you 'zrok enable'?", nil)
2023-01-09 20:16:08 +01:00
}
zrok, err := env.Client()
2022-11-30 19:45:57 +01:00
if err != nil {
if !panicInstead {
2023-01-09 23:48:19 +01:00
tui.Error("unable to create zrok client", err)
2022-11-30 19:45:57 +01:00
}
panic(err)
}
2023-01-09 20:16:08 +01:00
auth := httptransport.APIKeyAuth("X-TOKEN", "header", env.Environment().Token)
req := share.NewUnshareParams()
2022-11-30 19:45:57 +01:00
req.Body = &rest_model_zrok.UnshareRequest{
EnvZID: env.Environment().ZitiIdentity,
2023-01-04 20:42:58 +01:00
ShrToken: shrToken,
2022-11-30 19:45:57 +01:00
Reserved: true,
}
if _, err := zrok.Share.Unshare(req, auth); err != nil {
if !panicInstead {
2023-01-09 23:48:19 +01:00
tui.Error("error releasing share", err)
}
panic(err)
2022-11-30 19:45:57 +01:00
}
2023-01-04 20:42:58 +01:00
logrus.Infof("reserved share '%v' released", shrToken)
2022-11-30 19:45:57 +01:00
}