-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add `short` command with https://clck.ru as backend for create a short URL
- Loading branch information
Showing
3 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |