// Package root contains the root command for authctl.
package root

import (
	"github.com/canonical/authd/cmd/authctl/group"
	"github.com/canonical/authd/cmd/authctl/user"
	"github.com/spf13/cobra"
)

// RootCmd is the root command for authctl.
var RootCmd = &cobra.Command{
	Use:   "authctl",
	Short: "Manage authd users and groups",
	Long:  "authctl is a command-line tool for managing users and groups handled by authd.",
	PersistentPreRun: func(cmd *cobra.Command, args []string) {
		// The command was successfully parsed, so we don't want cobra to print usage information on error.
		cmd.SilenceUsage = true
	},
	CompletionOptions: cobra.CompletionOptions{
		HiddenDefaultCmd: true,
	},
	// Avoid the "Auto generated by spf13/cobra" line in the generated markdown docs
	DisableAutoGenTag: true,
	// We handle errors ourselves
	SilenceErrors: true,
	Args:          cobra.NoArgs,
	RunE:          func(cmd *cobra.Command, args []string) error { return cmd.Usage() },
}

func init() {
	// Disable command sorting by name. This makes cobra print the commands in the
	// order they are added to the root command and adds the `help` and `completion`
	// commands at the end.
	cobra.EnableCommandSorting = false

	RootCmd.AddCommand(user.UserCmd)
	RootCmd.AddCommand(group.GroupCmd)
}
