polycule-connect/polyculeconnect/cmd/backend/add.go

74 lines
2.2 KiB
Go
Raw Normal View History

package cmd
import (
"fmt"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/services/backend"
"github.com/spf13/cobra"
)
var (
backendID string
backendName string
backendIssuer string
backendClientID string
backendClientSecret string
)
var backendAddCmd = &cobra.Command{
Use: "add",
Short: "Add a new backend to the storage",
Long: `Add a new backend to the storage.
Parameters to provide:
- id: Unique ID to represent the backend in the storage
- name: Human readable name to represent the backend. It will be used by
the user in the authentication page to select a backend during
authentication
- issuer: Full hostname of the OIDC provider, e.g. 'https://github.com'
- client-id: OIDC Client ID for the backend
- client-secret OIDC Client secret for the backend`,
Run: func(cmd *cobra.Command, args []string) {
addNewBackend()
},
}
func addNewBackend() {
c := utils.InitConfig("")
logger.Init(c.LogLevel)
s := utils.InitStorage(c)
if backendClientID == "" {
utils.Fail("Empty client ID")
}
if backendClientSecret == "" {
utils.Fail("Empty client secret")
}
backendConf := backend.BackendConfig{
Issuer: backendIssuer,
ClientID: backendClientID,
ClientSecret: backendClientSecret,
RedirectURI: c.RedirectURI(),
ID: backendID,
Name: backendName,
}
if err := backend.New(s).AddBackend(backendConf); err != nil {
utils.Failf("Failed to add new backend to storage: %s", err.Error())
}
fmt.Printf("New backend %s added.\n", backendName)
}
func init() {
backendCmd.AddCommand(backendAddCmd)
backendAddCmd.Flags().StringVarP(&backendID, "id", "i", "", "ID to identify the backend in the storage")
backendAddCmd.Flags().StringVarP(&backendName, "name", "n", "", "Name to represent the backend")
backendAddCmd.Flags().StringVarP(&backendIssuer, "issuer", "d", "", "Full hostname of the backend")
backendAddCmd.Flags().StringVarP(&backendClientID, "client-id", "", "", "OIDC Client ID for the backend")
backendAddCmd.Flags().StringVarP(&backendClientSecret, "client-secret", "", "", "OIDC Client secret for the backend")
}