Skip to content

Commit

Permalink
Merge pull request lxc#560 from stgraber/cli
Browse files Browse the repository at this point in the history
Add `incus remote generate-certificate`
  • Loading branch information
hallyn authored Feb 29, 2024
2 parents 9af3a02 + 19504e0 commit ffcc473
Show file tree
Hide file tree
Showing 12 changed files with 10,055 additions and 9,886 deletions.
49 changes: 49 additions & 0 deletions cmd/incus/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (c *cmdRemote) Command() *cobra.Command {
remoteAddCmd := cmdRemoteAdd{global: c.global, remote: c}
cmd.AddCommand(remoteAddCmd.Command())

// Generate certificate
remoteGenerateCertificateCmd := cmdRemoteGenerateCertificate{global: c.global, remote: c}
cmd.AddCommand(remoteGenerateCertificateCmd.Command())

// Get default
remoteGetDefaultCmd := cmdRemoteGetDefault{global: c.global, remote: c}
cmd.AddCommand(remoteGetDefaultCmd.Command())
Expand Down Expand Up @@ -612,6 +616,51 @@ func (c *cmdRemoteAdd) Run(cmd *cobra.Command, args []string) error {
return conf.SaveConfig(c.global.confPath)
}

// Generate certificate.
type cmdRemoteGenerateCertificate struct {
global *cmdGlobal
remote *cmdRemote
}

func (c *cmdRemoteGenerateCertificate) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("generate-certificate")
cmd.Short = i18n.G("Generate the client certificate")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Manually trigger the generation of a client certificate`))

cmd.RunE = c.Run

return cmd
}

func (c *cmdRemoteGenerateCertificate) Run(cmd *cobra.Command, args []string) error {
conf := c.global.conf

// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 0, 0)
if exit {
return err
}

// Check if we already have a certificate.
if conf.HasClientCertificate() {
return fmt.Errorf(i18n.G("A client certificate is already present"))
}

// Generate the certificate.
if !c.global.flagQuiet {
fmt.Fprintf(os.Stderr, i18n.G("Generating a client certificate. This may take a minute...")+"\n")
}

err = conf.GenerateClientCertificate()
if err != nil {
return err
}

return nil
}

// Get default.
type cmdRemoteGetDefault struct {
global *cmdGlobal
Expand Down
Loading

0 comments on commit ffcc473

Please sign in to comment.