ccclip/cmd/cli/registerDevice.go
jmug 8445a7546e
Some checks failed
Fly Deploy / Deploy app (push) Has been cancelled
Allow registering device without requiring the creation of the config file.
Signed-off-by: jmug <u.g.a.mariano@gmail.com>
2025-05-10 14:26:20 -07:00

63 lines
1.4 KiB
Go

package main
import (
"errors"
"github.com/spf13/cobra"
"github.com/AYM1607/ccclip/internal/configfile"
"github.com/AYM1607/ccclip/pkg/crypto"
"github.com/AYM1607/ccclip/pkg/input"
)
func init() {
rootCmd.AddCommand(registerDeviceCommand)
registerDeviceCommand.Flags().StringVarP(&email, "email", "e", "", "email is your login identifier, ignored if your config file has has an email")
}
var registerDeviceCommand = &cobra.Command{
Use: "register-device",
Short: "Register a device for the given user",
RunE: func(cmd *cobra.Command, args []string) error {
cc, err := configfile.EnsureAndGet()
if err != nil {
return err
}
if cc.Email != "" {
email = cc.Email
}
if email == "" {
return errors.New("provide an email through your config file or with --email")
}
if cc.DeviceId != "" {
return errors.New("this device is already registered")
}
pvk := crypto.NewPrivateKey()
pbk := pvk.PublicKey()
password := input.ReadPassword()
res, err := apiclient.RegisterDevice(email, password, pbk.Bytes())
if err != nil {
return err
}
// Write the key files first, if those fail to write then we should not
// save the device Id.
cc.DeviceId = res.DeviceID
if cc.Email == "" {
cc.Email = email
}
err = configfile.SavePrivateKey(pvk)
if err != nil {
return err
}
err = configfile.SavePublicKey(pbk)
if err != nil {
return err
}
return configfile.Write(cc)
},
}