Skip to content

Commit

Permalink
upd: Add short command
Browse files Browse the repository at this point in the history
Add `short` command with https://clck.ru as backend for create a short
URL
  • Loading branch information
jtprogru committed Nov 21, 2024
1 parent 6756e35 commit cb95940
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Available Commands:
dupl Show all duplicates JPG and PNG in folder
help Help about any command
passwd Generate random password
short Make short link from URL
sretask Create template with SRE task
uuid Generate UUID string

Flags:
--config string config file (default is $HOME/.gch.yaml)
Expand Down Expand Up @@ -59,4 +59,3 @@ go install github.com/jtprogru/gch@$VERSION
## License

[MIT](LICENSE)

40 changes: 40 additions & 0 deletions cmd/short.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmd

import (
"fmt"

"github.com/jtprogru/gch/internal/clckru"
"github.com/spf13/cobra"
)

const (
defaultUtmTags = "utm_source=gch&utm_medium=console"
)

// shortCmd represents the short command
var shortCmd = &cobra.Command{
Use: "short",
Short: "Make short link from URL",
Long: `Create short link from URL.
As backend now used https://clck.ru
Usage: gch short <url> [utm_tag=utm_value]`,
Run: func(cmd *cobra.Command, args []string) {
// Check if a URL is provided as an argument
if len(args) < 1 {
fmt.Println("Usage: gch short <url>")
return
}

longUrl := args[0]
res, err := clckru.Shorten(longUrl, defaultUtmTags)
if err != nil {
fmt.Printf("error: %v\n", err)
}
fmt.Println(res)
},
}

func init() {
rootCmd.AddCommand(shortCmd)
}
46 changes: 46 additions & 0 deletions internal/clckru/clckru.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package clckru

import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
)

const (
// Endpoint for the URL shortening service
endpoint = "https://clck.ru/--"
)

func Shorten(longUrl string, utmTags string) (string, error) {
fullURL := longUrl
if utmTags != "" {
fullURL = fmt.Sprintf("%s?%s", longUrl, utmTags)
}
data := url.Values{}
data.Set("url", fullURL)
req, err := http.NewRequest("POST", endpoint, bytes.NewBufferString(data.Encode()))
if err != nil {
return "", fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected response status: %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %w", err)
}

// Возвращаем результат в виде строки
return string(body), nil
}

0 comments on commit cb95940

Please sign in to comment.