package main import ( "encoding/json" "errors" "fmt" "isle/bootstrap" "isle/daemon/network" "os" "sort" ) var subCmdHostCreate = subCmd{ name: "create", descr: "Creates a new host in the network, writing its new bootstrap.json to stdout", do: func(ctx subCmdCtx) error { var ( hostName hostNameFlag ip ipFlag ) hostNameF := ctx.flags.VarPF( &hostName, "hostname", "n", "Name of the host to generate bootstrap.json for", ) ctx.flags.VarP(&ip, "ip", "i", "IP of the new host. An available IP will be chosen if none is given.") canCreateHosts := ctx.flags.Bool( "can-create-hosts", false, "The new host should have the ability to create hosts too", ) ctx, err := ctx.withParsedFlags() if err != nil { return fmt.Errorf("parsing flags: %w", err) } if !hostNameF.Changed { return errors.New("--hostname is required") } res, err := newDaemonRPCClient().CreateHost( ctx, hostName.V, network.CreateHostOpts{ IP: ip.V, CanCreateHosts: *canCreateHosts, }, ) if err != nil { return fmt.Errorf("calling CreateHost: %w", err) } return json.NewEncoder(os.Stdout).Encode(res) }, } var subCmdHostList = subCmd{ name: "list", descr: "Lists all hosts in the network, and their IPs", do: doWithOutput(func(ctx subCmdCtx) (any, error) { ctx, err := ctx.withParsedFlags() if err != nil { return nil, fmt.Errorf("parsing flags: %w", err) } hostsRes, err := ctx.getHosts() if err != nil { return nil, fmt.Errorf("calling GetHosts: %w", err) } type host struct { Name string VPN struct { IP string } Storage bootstrap.GarageHost `json:",omitempty"` } hosts := make([]host, 0, len(hostsRes)) for _, h := range hostsRes { host := host{ Name: string(h.Name), Storage: h.Garage, } host.VPN.IP = h.IP().String() hosts = append(hosts, host) } sort.Slice(hosts, func(i, j int) bool { return hosts[i].Name < hosts[j].Name }) return hosts, nil }), } var subCmdHostRemove = subCmd{ name: "remove", descr: "Removes a host from the network", do: func(ctx subCmdCtx) error { var ( hostName hostNameFlag ) hostNameF := ctx.flags.VarPF( &hostName, "hostname", "n", "Name of the host to remove", ) ctx, err := ctx.withParsedFlags() if err != nil { return fmt.Errorf("parsing flags: %w", err) } if !hostNameF.Changed { return errors.New("--hostname is required") } if err := newDaemonRPCClient().RemoveHost(ctx, hostName.V); err != nil { return fmt.Errorf("calling RemoveHost: %w", err) } return nil }, } var subCmdHost = subCmd{ name: "host", plural: "s", descr: "Sub-commands having to do with configuration of hosts in the network", do: func(ctx subCmdCtx) error { return ctx.doSubCmd( subCmdHostCreate, subCmdHostRemove, subCmdHostList, ) }, }