agentcmd

package
v0.14.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConnectCmd = &cobra.Command{
	Use:   "connect <server>",
	Short: "Connect to a server",
	Long:  `Authenticate the client with a remote server and save the server address and access key.`,
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		var token string

		server := args[0]

		if !strings.HasPrefix(server, "http://") && !strings.HasPrefix(server, "https://") {
			server = "https://" + server
		}

		fmt.Println("Connecting to server: ", server)

		u, err := url.Parse(server)
		if err != nil {
			fmt.Println("Failed to parse server URL")
			os.Exit(1)
		}

		hostname, err := os.Hostname()
		if err != nil {
			fmt.Println("Failed to get hostname")
			os.Exit(1)
		}

		hostname = "knot client " + hostname

		if cmd.Flags().Lookup("use-web-auth").Value.String() == "true" {

			u.Path = "/api-tokens/create/" + url.PathEscape(hostname)

			err = open(u.String())
			if err != nil {
				fmt.Println("Failed to open server URL, you will need to generate the API token manually")
				os.Exit(1)
			}

			fmt.Print("Enter token: ")
			_, err = fmt.Scanln(&token)
			if err != nil {
				fmt.Println("Failed to read token, you will need to generate the API token manually")
				os.Exit(1)
			}
		} else {
			var username string = cmd.Flags().Lookup("username").Value.String()
			var password []byte
			var err error

			if username == "" {

				fmt.Print("Enter email: ")
				_, err = fmt.Scanln(&username)
				if err != nil {
					fmt.Println("Failed to read email address")
					os.Exit(1)
				}
			}

			fmt.Print("Enter password: ")
			password, err = term.ReadPassword(int(syscall.Stdin))
			if err != nil {
				fmt.Println("Failed to read password")
				os.Exit(1)
			}
			fmt.Println()

			if username == "" || string(password) == "" {
				fmt.Println("Username and password must be given")
				os.Exit(1)
			}

			client := apiclient.NewClient(server, "", cmd.Flags().Lookup("tls-skip-verify").Value.String() == "true")
			sessionToken, _, _ := client.Login(username, string(password))
			if sessionToken == "" {
				fmt.Println("Failed to login")
				os.Exit(1)
			}

			client.UseSessionCookie(true).SetAuthToken(sessionToken)

			token, _, err = client.CreateToken(hostname)
			if err != nil || token == "" {
				fmt.Println("Failed to create token")
				os.Exit(1)
			}
		}

		if viper.ConfigFileUsed() == "" {

			home, err := os.UserHomeDir()
			cobra.CheckErr(err)

			partial := viper.New()
			partial.Set("client.server", server)
			partial.Set("client.token", token)

			err = partial.WriteConfigAs(home + "/" + config.CONFIG_FILE_NAME + "." + config.CONFIG_FILE_TYPE)
			if err != nil {
				fmt.Println("Failed to create config file")
				os.Exit(1)
			}
		} else {
			partial := viper.New()
			partial.SetConfigFile(viper.ConfigFileUsed())
			err = partial.ReadInConfig()
			if err != nil {
				fmt.Println("Failed to read config file")
				os.Exit(1)
			}

			partial.Set("client.server", server)
			partial.Set("client.token", token)

			err = partial.WriteConfig()
			if err != nil {
				fmt.Println("Failed to save config file")
				os.Exit(1)
			}
		}
	},
}
View Source
var (
	RootCmd = &cobra.Command{
		Use:   "knot-agent",
		Short: "knot agent to connect the environment to the knot server",
		Long: `knot is a management tool for developer environments running within a Nomad cluster.

The agent connects environments to the knot server.`,
		Version: build.Version,
		Run: func(cmd *cobra.Command, args []string) {
			cmd.Help()
		},
	}
)

Functions

func Execute

func Execute()

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL