mirror of
				https://github.com/juanfont/headscale.git
				synced 2025-11-04 01:51:04 +01:00 
			
		
		
		
	feat: updating cli to match the set command
This commit is contained in:
		
							parent
							
								
									25f1dcf724
								
							
						
					
					
						commit
						cc9eeda889
					
				@ -48,23 +48,14 @@ func init() {
 | 
				
			|||||||
	nodeCmd.AddCommand(deleteNodeCmd)
 | 
						nodeCmd.AddCommand(deleteNodeCmd)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	nodeCmd.AddCommand(tagCmd)
 | 
						nodeCmd.AddCommand(tagCmd)
 | 
				
			||||||
	addTagCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
 | 
						setTagCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
 | 
				
			||||||
	err = addTagCmd.MarkFlagRequired("identifier")
 | 
						err = setTagCmd.MarkFlagRequired("identifier")
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Fatalf(err.Error())
 | 
							log.Fatalf(err.Error())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	addTagCmd.Flags().
 | 
						setTagCmd.Flags().
 | 
				
			||||||
		StringSliceP("tags", "t", []string{}, "List of tags to add to the node")
 | 
							StringSliceP("tags", "t", []string{}, "List of tags to add to the node")
 | 
				
			||||||
	tagCmd.AddCommand(addTagCmd)
 | 
						tagCmd.AddCommand(setTagCmd)
 | 
				
			||||||
 | 
					 | 
				
			||||||
	delTagCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
 | 
					 | 
				
			||||||
	err = delTagCmd.MarkFlagRequired("identifier")
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		log.Fatalf(err.Error())
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	delTagCmd.Flags().
 | 
					 | 
				
			||||||
		StringSliceP("tags", "t", []string{}, "List of tags to remove from the node")
 | 
					 | 
				
			||||||
	tagCmd.AddCommand(delTagCmd)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var nodeCmd = &cobra.Command{
 | 
					var nodeCmd = &cobra.Command{
 | 
				
			||||||
@ -425,9 +416,9 @@ var tagCmd = &cobra.Command{
 | 
				
			|||||||
	Aliases: []string{"t", "tag"},
 | 
						Aliases: []string{"t", "tag"},
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var addTagCmd = &cobra.Command{
 | 
					var setTagCmd = &cobra.Command{
 | 
				
			||||||
	Use:   "add",
 | 
						Use:   "set",
 | 
				
			||||||
	Short: "Add tags to a node in your network",
 | 
						Short: "set tags to a node in your network",
 | 
				
			||||||
	Run: func(cmd *cobra.Command, args []string) {
 | 
						Run: func(cmd *cobra.Command, args []string) {
 | 
				
			||||||
		output, _ := cmd.Flags().GetString("output")
 | 
							output, _ := cmd.Flags().GetString("output")
 | 
				
			||||||
		ctx, client, conn, cancel := getHeadscaleCLIClient()
 | 
							ctx, client, conn, cancel := getHeadscaleCLIClient()
 | 
				
			||||||
@ -445,7 +436,7 @@ var addTagCmd = &cobra.Command{
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		tagsToAdd, err := cmd.Flags().GetStringSlice("tags")
 | 
							tagsToSet, err := cmd.Flags().GetStringSlice("tags")
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			ErrorOutput(
 | 
								ErrorOutput(
 | 
				
			||||||
				err,
 | 
									err,
 | 
				
			||||||
@ -456,129 +447,23 @@ var addTagCmd = &cobra.Command{
 | 
				
			|||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// retrieve machine informations
 | 
							// Sending tags to machine
 | 
				
			||||||
		request := &v1.GetMachineRequest{
 | 
							request := &v1.SetTagsRequest{
 | 
				
			||||||
			MachineId: identifier,
 | 
								MachineId: identifier,
 | 
				
			||||||
 | 
								Tags:      tagsToSet,
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		resp, err := client.GetMachine(ctx, request)
 | 
							resp, err := client.SetTags(ctx, request)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			ErrorOutput(
 | 
								ErrorOutput(
 | 
				
			||||||
				err,
 | 
									err,
 | 
				
			||||||
				fmt.Sprintf("Error retrieving machine: %s", err),
 | 
									fmt.Sprintf("Error while sending tags to headscale: %s", err),
 | 
				
			||||||
				output,
 | 
									output,
 | 
				
			||||||
			)
 | 
								)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// update machine
 | 
							if resp != nil {
 | 
				
			||||||
		mergedTags := resp.Machine.GetForcedTags()
 | 
					 | 
				
			||||||
		for _, tag := range tagsToAdd {
 | 
					 | 
				
			||||||
			if !containsString(mergedTags, tag) {
 | 
					 | 
				
			||||||
				mergedTags = append(mergedTags, tag)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		machine := resp.GetMachine()
 | 
					 | 
				
			||||||
		machine.ForcedTags = mergedTags
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		updateReq := &v1.UpdateMachineRequest{
 | 
					 | 
				
			||||||
			Machine: machine,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// send updated machine upstream
 | 
					 | 
				
			||||||
		updateResponse, err := client.UpdateMachine(ctx, updateReq)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			ErrorOutput(
 | 
					 | 
				
			||||||
				err,
 | 
					 | 
				
			||||||
				fmt.Sprintf("Error while updating machine: %s", err),
 | 
					 | 
				
			||||||
				output,
 | 
					 | 
				
			||||||
			)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if updateResponse != nil {
 | 
					 | 
				
			||||||
			SuccessOutput(
 | 
								SuccessOutput(
 | 
				
			||||||
				updateResponse.GetMachine(),
 | 
									resp.GetMachine(),
 | 
				
			||||||
				"Machine updated",
 | 
					 | 
				
			||||||
				output,
 | 
					 | 
				
			||||||
			)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	},
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var delTagCmd = &cobra.Command{
 | 
					 | 
				
			||||||
	Use:     "del",
 | 
					 | 
				
			||||||
	Short:   "remove tags to a node in your network",
 | 
					 | 
				
			||||||
	Aliases: []string{"remove", "rm"},
 | 
					 | 
				
			||||||
	Run: func(cmd *cobra.Command, args []string) {
 | 
					 | 
				
			||||||
		output, _ := cmd.Flags().GetString("output")
 | 
					 | 
				
			||||||
		ctx, client, conn, cancel := getHeadscaleCLIClient()
 | 
					 | 
				
			||||||
		defer cancel()
 | 
					 | 
				
			||||||
		defer conn.Close()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// retrieve flags from CLI
 | 
					 | 
				
			||||||
		identifier, err := cmd.Flags().GetUint64("identifier")
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			ErrorOutput(
 | 
					 | 
				
			||||||
				err,
 | 
					 | 
				
			||||||
				fmt.Sprintf("Error converting ID to integer: %s", err),
 | 
					 | 
				
			||||||
				output,
 | 
					 | 
				
			||||||
			)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		tagsToRemove, err := cmd.Flags().GetStringSlice("tags")
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			ErrorOutput(
 | 
					 | 
				
			||||||
				err,
 | 
					 | 
				
			||||||
				fmt.Sprintf("Error retrieving list of tags to add to machine: %v", err),
 | 
					 | 
				
			||||||
				output,
 | 
					 | 
				
			||||||
			)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// retrieve machine informations
 | 
					 | 
				
			||||||
		request := &v1.GetMachineRequest{
 | 
					 | 
				
			||||||
			MachineId: identifier,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		resp, err := client.GetMachine(ctx, request)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			ErrorOutput(
 | 
					 | 
				
			||||||
				err,
 | 
					 | 
				
			||||||
				fmt.Sprintf("Error retrieving machine: %s", err),
 | 
					 | 
				
			||||||
				output,
 | 
					 | 
				
			||||||
			)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// update machine
 | 
					 | 
				
			||||||
		keepTags := resp.Machine.GetForcedTags()
 | 
					 | 
				
			||||||
		for _, tag := range tagsToRemove {
 | 
					 | 
				
			||||||
			for i, t := range keepTags {
 | 
					 | 
				
			||||||
				if t == tag {
 | 
					 | 
				
			||||||
					keepTags = append(keepTags[:i], keepTags[i+1:]...)
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		machine := resp.GetMachine()
 | 
					 | 
				
			||||||
		machine.ForcedTags = keepTags
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		updateReq := &v1.UpdateMachineRequest{
 | 
					 | 
				
			||||||
			Machine: machine,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// send updated machine upstream
 | 
					 | 
				
			||||||
		updateResponse, err := client.UpdateMachine(ctx, updateReq)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			ErrorOutput(
 | 
					 | 
				
			||||||
				err,
 | 
					 | 
				
			||||||
				fmt.Sprintf("Error while updating machine: %s", err),
 | 
					 | 
				
			||||||
				output,
 | 
					 | 
				
			||||||
			)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if updateResponse != nil {
 | 
					 | 
				
			||||||
			SuccessOutput(
 | 
					 | 
				
			||||||
				updateResponse.GetMachine(),
 | 
					 | 
				
			||||||
				"Machine updated",
 | 
									"Machine updated",
 | 
				
			||||||
				output,
 | 
									output,
 | 
				
			||||||
			)
 | 
								)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user