Skip to content

Commit b5dcc9e

Browse files
committed
init: push source code of project
0 parents  commit b5dcc9e

File tree

6 files changed

+135
-0
lines changed

6 files changed

+135
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
http-cat-cli

LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2+
Version 2, December 2004
3+
4+
Copyright (C) 2004 Sam Hocevar <[email protected]>
5+
6+
Everyone is permitted to copy and distribute verbatim or modified
7+
copies of this license document, and changing it is allowed as long
8+
as the name is changed.
9+
10+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12+
13+
0. You just DO WHAT THE FUCK YOU WANT TO.

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build:
2+
go build
3+
4+
install:
5+
sudo cp ./http-cat-cli /usr/bin/http-cat

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module devgoldy.xyz/http-cat-cli
2+
3+
go 1.23.0
4+
5+
require github.com/urfave/cli/v2 v2.27.4
6+
7+
require (
8+
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
9+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
10+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
11+
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
2+
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
3+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
4+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
5+
github.com/urfave/cli/v2 v2.27.4 h1:o1owoI+02Eb+K107p27wEX9Bb8eqIoZCfLXloLUSWJ8=
6+
github.com/urfave/cli/v2 v2.27.4/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ=
7+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
8+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=

httpcat.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/urfave/cli/v2"
6+
"io"
7+
"log"
8+
"log/slog"
9+
"net/http"
10+
"os"
11+
"os/exec"
12+
"path/filepath"
13+
"strconv"
14+
)
15+
16+
const apiHost string = "https://http.cat"
17+
18+
func main() {
19+
app := &cli.App{
20+
Name: "http-cat",
21+
Usage: "Returns a HTTP 🐱 kitty!",
22+
Action: func(cCtx *cli.Context) error {
23+
var http_status_code string = cCtx.Args().Get(0)
24+
25+
if !isStringNumber(http_status_code) {
26+
fmt.Println(
27+
"You must provide a HTTP status code! " +
28+
"E.g. http-cat 404, http-cat 500. Run 'http-cat --help' for more help.",
29+
)
30+
return nil
31+
}
32+
33+
slog.Debug("Making request to HTTP Cat API...")
34+
35+
response, err := http.Get(apiHost + "/" + http_status_code)
36+
37+
if err != nil {
38+
log.Fatalln("GET request to HTTP Cat API failed:", err)
39+
}
40+
41+
var temp_directory_path string = getTempDir()
42+
43+
if _, err := os.Stat(temp_directory_path); os.IsNotExist(err) {
44+
_ = os.Mkdir(temp_directory_path, 0777) // everyone can read and write
45+
}
46+
47+
var path_to_image string = filepath.Join(getTempDir(), "kitty.jpeg")
48+
49+
body, err := io.ReadAll(response.Body)
50+
if err != nil {
51+
log.Fatalln("Failed to read image from API:", err)
52+
}
53+
54+
err = os.WriteFile(path_to_image, body, 0777)
55+
56+
if err != nil {
57+
log.Fatalln("Failed to write the 🐈 kitty in the temp directory:", err)
58+
}
59+
60+
output, err := exec.Command("chafa", path_to_image, "--scale", "0.7").Output()
61+
62+
if err != nil {
63+
log.Fatalln(
64+
"Failed to execute chafa to display 🐈 kitty! "+
65+
"Make sure you have it installed and in path. Error:", err,
66+
)
67+
}
68+
69+
fmt.Print(string(output))
70+
71+
return nil
72+
},
73+
}
74+
75+
if err := app.Run(os.Args); err != nil {
76+
log.Fatal(err)
77+
}
78+
}
79+
80+
func isStringNumber(string string) bool {
81+
if _, err := strconv.Atoi(string); err == nil {
82+
return true
83+
}
84+
85+
return false
86+
}
87+
88+
// Currently only supports Linux, fuck you Windows, no kitties for you for the time being.
89+
func getTempDir() string {
90+
var temp_directory string = os.Getenv("TMPDIR")
91+
92+
if temp_directory == "" {
93+
temp_directory = "/tmp"
94+
}
95+
96+
return filepath.Join(temp_directory, "http-cat")
97+
}

0 commit comments

Comments
 (0)