Skip to content

Commit

Permalink
feat : read config.ini file
Browse files Browse the repository at this point in the history
  • Loading branch information
LordPax committed Aug 23, 2024
1 parent 8176de7 commit ca05c1c
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ai
aicli
tags
vendor
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

### Added

* Add this cli template
* Add localized for `en` and `fr`
* Read config.ini file
5 changes: 3 additions & 2 deletions commands/base.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package commands

import (
"ai/lang"
"ai/utils"
"fmt"

"github.com/LordPax/aicli/lang"
"github.com/LordPax/aicli/utils"

cli "github.com/urfave/cli/v2"
)

Expand Down
34 changes: 0 additions & 34 deletions commands/test.go

This file was deleted.

41 changes: 41 additions & 0 deletions commands/text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package commands

import (
"fmt"

"github.com/LordPax/aicli/lang"
"github.com/LordPax/aicli/service"
"github.com/LordPax/aicli/utils"

cli "github.com/urfave/cli/v2"
)

func TextCommand() *cli.Command {
l := lang.GetLocalize()
return &cli.Command{
Name: "text",
Usage: l.Get("text-usage"),
ArgsUsage: "[prompt]",
Aliases: []string{"t"},
Action: testAction,
}
}

func testAction(c *cli.Context) error {
l := lang.GetLocalize()
log, _ := utils.GetLog()

if c.NArg() == 0 {
return fmt.Errorf(l.Get("no-args"))
}

prompt := c.Args().First()
response, err := service.SendTextRequest(prompt)
if err != nil {
return err
}

log.Printf(l.Get("hello-world"), response)

return nil
}
14 changes: 12 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package config

import (
"ai/utils"
"fmt"
"os"
"path"

"github.com/LordPax/aicli/utils"

ini "gopkg.in/ini.v1"
)

var home, _ = os.UserHomeDir()
Expand All @@ -15,7 +18,14 @@ var (
CONFIG_DIR = path.Join(home, ".config", "aicli")
CONFIG_FILE = path.Join(CONFIG_DIR, "config.ini")
LOG_FILE = path.Join(CONFIG_DIR, "log")
CONFIG_EXEMPLE = `# Configuration file for aicli`
CONFIG_INI *ini.File
CONFIG_EXEMPLE = `# Configuration file for aicli
[text]
type=openai
route=https://api.openai.com/v1
model=gpt4
api_key=yoursecretapikey
temp=0.7`
)

func InitConfig() error {
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module ai
module github.com/LordPax/aicli

go 1.22.6

require (
github.com/joho/godotenv v1.5.1
github.com/urfave/cli/v2 v2.27.3
gopkg.in/ini.v1 v1.67.0
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
)
12 changes: 10 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/urfave/cli/v2 v2.27.3 h1:/POWahRmdh7uztQ3CYnaDddk0Rm90PyOgIxgW2rr41M=
github.com/urfave/cli/v2 v2.27.3/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1 change: 1 addition & 0 deletions lang/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ var EN_STRINGS = LangString{
"hello-world": "Hello, world %s\n",
"no-args": "No arguments provided",
"no-command": "No command provided",
"text-usage": "Generate text from a prompt",
}
1 change: 1 addition & 0 deletions lang/fr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ var FR_STRINGS = LangString{
"hello-world": "Bonjour, monde %s\n",
"no-args": "Aucun argument fourni",
"no-command": "Aucune commande fournie",
"text-usage": "Générer du texte à partir d'un prompt",
}
16 changes: 9 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package main

import (
"ai/commands"
"ai/config"
"ai/lang"
"ai/utils"
"fmt"
"os"

"github.com/joho/godotenv"
"github.com/LordPax/aicli/commands"
"github.com/LordPax/aicli/config"
"github.com/LordPax/aicli/lang"
"github.com/LordPax/aicli/utils"

cli "github.com/urfave/cli/v2"
ini "gopkg.in/ini.v1"
)

func main() {
Expand All @@ -25,7 +26,8 @@ func main() {
}
defer log.Close()

if err := godotenv.Load(config.CONFIG_FILE); err != nil {
config.CONFIG_INI, err = ini.Load(config.CONFIG_FILE)
if err != nil {
log.PrintfErr("%v\n", err)
os.Exit(1)
}
Expand All @@ -42,7 +44,7 @@ func main() {
app.Action = commands.MainAction
app.Flags = commands.MainFlags()
app.Commands = []*cli.Command{
commands.TestCommand(),
commands.TextCommand(),
}

if err := app.Run(os.Args); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions service/text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package service

func SendTextRequest(text string) (string, error) {
return text, nil
}

0 comments on commit ca05c1c

Please sign in to comment.