This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
url.go
73 lines (65 loc) · 1.74 KB
/
url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/ahelmy/xdev/internal"
"github.com/spf13/cobra"
)
// urlCmd represents the url command
var urlCmd = &cobra.Command{
Use: "url",
Short: "URL encode/decode.",
Long: `URL encode/decode. For example:
encode a URL string (default).
decode a URL string.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cmd.Help()
return
}
},
}
var urlEncodeCmd = &cobra.Command{
Use: "encode",
Short: "URL encode a string.",
Long: `URL encode a string. For example:
encode "https://www.google.com/search?q=hello+world"`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Please provide a URL string to encode")
return
}
fmt.Println(internal.EncodeURL(args[0]))
},
}
var urlDecodeCmd = &cobra.Command{
Use: "decode",
Short: "URL decode a string.",
Long: `URL decode a string. For example:
decode "https%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Dhello%2Bworld"`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Please provide a URL string to decode")
return
}
decoded, err := internal.DecodeURL(args[0])
if err != nil {
fmt.Println(err)
return
}
fmt.Println(decoded)
},
}
func init() {
rootCmd.AddCommand(urlCmd)
urlCmd.AddCommand(urlEncodeCmd, urlDecodeCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// urlCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// urlCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}