Skip to content

Commit

Permalink
Merge pull request #4 from yossydev/fix/command
Browse files Browse the repository at this point in the history
Fix/command
  • Loading branch information
Yuto Yoshino authored Oct 29, 2023
2 parents 3e1ac77 + e0c6256 commit b6946a2
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 66 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Bump Homebrew Formula
on:
push:
branches: [main]
jobs:
homebrew:
name: Bump Homebrew formula
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Extract version
id: extract-version
run: |
echo "tag-name=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
- name: Bump Homebrew formula
uses: mislav/bump-homebrew-formula-action@v3
with:
formula-name: rando_lgtm
formula-path: rando_lgtm.rb
homebrew-tap: yossydev/homebrew-RandoLGTM
base-branch: main
download-url: https://github.com/yossydev/RandoLGTM/archive/refs/tags/${{ steps.extract-version.outputs.tag-name }}.tar.gz
commit-message: |
rando_lgtm ${{ steps.extract-version.outputs.tag-name }}
Created by https://github.com/mislav/bump-homebrew-formula-action
env:
COMMITTER_TOKEN: ${{ secrets.COMMITTER_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/RandoLGTM
/messages.txt
/main
30 changes: 0 additions & 30 deletions cmd/addLgtm.go

This file was deleted.

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

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var addCmd = &cobra.Command{
Use: "add",
Short: "Add a new LGTM message",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := addMessageToFile(args[0])
if err != nil {
fmt.Println("Error: ", err)
return
}
fmt.Printf("Added new LGTM message: '%s'\n", args[0])
},
}

func addMessageToFile(message string) error {
file, err := os.OpenFile("messages.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to open file: %v", err)
}
defer file.Close()
_, err = file.WriteString(message + "\n")
if err != nil {
return fmt.Errorf("failed to write to file: %v", err)
}
return nil
}

func init() {
rootCmd.AddCommand(addCmd)
}
33 changes: 0 additions & 33 deletions cmd/getLgtm.go

This file was deleted.

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

import (
"fmt"
"math/rand"
"os"
"strings"
"time"
"github.com/spf13/cobra"
)

var emojis = []string{
"😀", "😃", "😄", "😁", "😆", "😅", "😂", "🤣", "😊", "😇",
"🙂", "🙃", "😉", "😌", "😍", "🥰", "😘", "😗", "😙", "😚",
"😋", "😛", "😝", "😜", "🤪", "🤨", "🧐", "🤓", "😎", "🥸",
"😏", "😒", "😫", "😩", "🥺", "😢", "😭", "😤", "🤯", "😳",
"🤗", "🤭", "🤫", "😬", "😴", "🤤", "🥴", "🤑", "🤠", "😈",
"👹", "👺", "🤡", "👻", "💀", "👽", "👾", "🤖", "🎃", "😺",
"😸", "😹", "😻", "😼", "😽",
}

var getCmd = &cobra.Command{
Use: "get",
Short: "Get a random LGTM message",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
message, err := getRandomMessageFromFile()
if err != nil {
fmt.Println("Error: ", err)
return
}
fmt.Println(message)
},
}

func surroundWithEmoji(s string) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
emoji := emojis[r.Intn(len(emojis))]
return emoji + s + emoji
}

var defaultMessages = []string{
"Looks good to me!",
"Great job!",
"Well done!",
"Keep up the good work!",
}

func getRandomMessageFromFile() (string, error) {
var messages []string
if _, err := os.Stat("messages.txt"); err == nil {
content, err := os.ReadFile("messages.txt")
if err != nil {
return "", fmt.Errorf("failed to read file: %v", err)
}
lines := strings.Split(string(content), "\n")
for _, line := range lines {
if line != "" {
messages = append(messages, line)
}
}
}
for _, message := range defaultMessages {
if message != "" {
messages = append(messages, message)
}
}
if len(messages) == 0 {
return "", fmt.Errorf("no messages available")
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randomMessage := messages[r.Intn(len(messages))]
return surroundWithEmoji(randomMessage), nil
}

func init() {
rootCmd.AddCommand(getCmd)
}

4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

var rootCmd = &cobra.Command{
Use: "RandoLGTM",
Use: "rando_lgtm",
Short: "LGTM message generator",
Long: `This is a CLI tool for generating and managing LGTM messages.`,
}
Expand All @@ -21,5 +21,5 @@ func Execute() {
}

func main() {
Execute()
Execute()
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21.0

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
Expand Down
2 changes: 1 addition & 1 deletion main.go
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
Copyright © 2023 NAME HERE [email protected]
*/
package main

Expand Down

0 comments on commit b6946a2

Please sign in to comment.