http-boot-config/config/cmd/discover.go

61 lines
1.7 KiB
Go
Raw Normal View History

2024-04-30 16:42:35 +00:00
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"errors"
"fmt"
"os"
"git.faercol.me/faercol/http-boot-config/config/config"
"git.faercol.me/faercol/http-boot-config/config/discover"
"github.com/spf13/cobra"
)
// discoverCmd represents the discover command
var discoverCmd = &cobra.Command{
Use: "discover",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
discoverRemoteServer()
},
}
func discoverRemoteServer() {
fmt.Println("Trying to autodiscover remote boot server")
conf, _ := config.Get()
remote, err := discover.DiscoverServer(conf.Discovery)
if err != nil {
if errors.Is(err, discover.ErrNoServer) {
fmt.Println("No remote boot server found on the network.")
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "Failed to discover server: %s\n", err.Error())
os.Exit(1)
}
fmt.Printf("Found remote boot server, server address is %s\n", remote)
}
func init() {
rootCmd.AddCommand(discoverCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// discoverCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// discoverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}