Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for a config file #48

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ jobs:
- name: Fetch github issue
run: go run cmd/summaraizer/summaraizer.go github --issue ioki-mobility/summaraizer/11

fetchGitHubIssueConfigFile:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'

- name: Write config file
run: |
echo '{ "github": { "issue": "ioki-mobility/summaraizer/11" } }' > config.json

- name: Fetch github issue
run: go run cmd/summaraizer/summaraizer.go github

summarizeOllama:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
config.json
18 changes: 17 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@ module github.com/ioki-mobility/summaraizer

go 1.22.2

require github.com/spf13/cobra v1.8.1
require (
github.com/knadh/koanf/parsers/json v0.1.0
github.com/knadh/koanf/providers/env v1.0.0
github.com/knadh/koanf/providers/file v1.1.2
github.com/knadh/koanf/v2 v2.1.2
github.com/spf13/cobra v1.8.1
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // indirect
golang.org/x/sys v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
37 changes: 36 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,49 @@
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/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs=
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
github.com/knadh/koanf/parsers/json v0.1.0 h1:dzSZl5pf5bBcW0Acnu20Djleto19T0CfHcvZ14NJ6fU=
github.com/knadh/koanf/parsers/json v0.1.0/go.mod h1:ll2/MlXcZ2BfXD6YJcjVFzhG9P0TdJ207aIBKQhV2hY=
github.com/knadh/koanf/providers/env v1.0.0 h1:ufePaI9BnWH+ajuxGGiJ8pdTG0uLEUWC7/HDDPGLah0=
github.com/knadh/koanf/providers/env v1.0.0/go.mod h1:mzFyRZueYhb37oPmC1HAv/oGEEuyvJDA98r3XAa8Gak=
github.com/knadh/koanf/providers/file v1.1.2 h1:aCC36YGOgV5lTtAFz2qkgtWdeQsgfxUkxDOe+2nQY3w=
github.com/knadh/koanf/providers/file v1.1.2/go.mod h1:/faSBcv2mxPVjFrXck95qeoyoZ5myJ6uxN8OOVNJJCI=
github.com/knadh/koanf/v2 v2.1.2 h1:I2rtLRqXRy1p01m/utEtpZSSA6dcJbgGVuE27kW2PzQ=
github.com/knadh/koanf/v2 v2.1.2/go.mod h1:Gphfaen0q1Fc1HTgJgSTC4oRX9R2R5ErYMZJy8fLJBo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
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/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
110 changes: 100 additions & 10 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ import (
)

func NewRootCmd() *cobra.Command {
conf := newConfig()
var rootCmd = &cobra.Command{
Use: "summaraizer",
Short: "Summarizes comments",
Long: "Summarizes comments from a variety of sources using AI models from different providers.",
}
rootCmd.AddCommand(githubCmd(), redditCmd(), gitlabCmd(), slackCmd())
rootCmd.AddCommand(ollamaCmd(), openaiCmd(), anthropicCmd(), googleCmd())
rootCmd.AddCommand(githubCmd(conf), redditCmd(conf), gitlabCmd(conf), slackCmd(conf))
rootCmd.AddCommand(ollamaCmd(conf), openaiCmd(conf), anthropicCmd(conf), googleCmd(conf))
return rootCmd
}

func githubCmd() *cobra.Command {
func githubCmd(c *config) *cobra.Command {
flagIssue := "issue"
flagToken := "token"
var cmd = &cobra.Command{
Expand All @@ -45,14 +46,22 @@ summaraizer github --issue ioki-mobility/summaraizer/1 --token <token>`,
return fetch(s)
},
}

cmd.Flags().String(flagIssue, "", "The GitHub issue to summarize. Use the format owner/repo/issue_number.")
cmd.MarkFlagRequired(flagIssue)
if issueConfig := c.GetString("github.issue"); issueConfig != "" {
cmd.Flags().Set(flagIssue, issueConfig)
}

cmd.Flags().String(flagToken, "", "The GitHub token. Only required for private repositories.")
if tokenConfig := c.GetString("github.token"); tokenConfig != "" {
cmd.Flags().Set(flagToken, tokenConfig)
}

return cmd
}

func redditCmd() *cobra.Command {
func redditCmd(c *config) *cobra.Command {
flagPost := "post"
cmd := &cobra.Command{
Use: "reddit",
Expand All @@ -69,12 +78,17 @@ Note that we only fetch the top-level comments. Nested comments are ignored.`,
return fetch(s)
},
}

cmd.Flags().String(flagPost, "", "The Reddit post to summarize. Use the URL path.")
cmd.MarkFlagRequired(flagPost)
if postConfig := c.GetString("reddit.post"); postConfig != "" {
cmd.Flags().Set(flagPost, postConfig)
}

return cmd
}

func gitlabCmd() *cobra.Command {
func gitlabCmd(c *config) *cobra.Command {
flagIssue := "issue"
flagToken := "token"
flagUrl := "url"
Expand Down Expand Up @@ -103,16 +117,28 @@ summaraizer gitlab --issue ioki-mobility/summaraizer/1 --token <token> --url htt
return fetch(s)
},
}

cmd.Flags().String(flagIssue, "", "The GitLab issue to summarize. Use the format owner/repo/issue_number.")
cmd.MarkFlagRequired(flagIssue)
if issueConfig := c.GetString("gitlab.issue"); issueConfig != "" {
cmd.Flags().Set(flagIssue, issueConfig)
}

cmd.Flags().String(flagToken, "", "The GitLab token.")
cmd.MarkFlagRequired(flagToken)
if tokenConfig := c.GetString("gitlab.token"); tokenConfig != "" {
cmd.Flags().Set(flagToken, tokenConfig)
}

cmd.Flags().String(flagUrl, "https://gitlab.com", "The URL of the GitLab instance.")
if urlConfig := c.GetString("gitlab.url"); urlConfig != "" {
cmd.Flags().Set(flagUrl, urlConfig)
}

return cmd
}

func slackCmd() *cobra.Command {
func slackCmd(c *config) *cobra.Command {
flagToken := "token"
flagChannel := "channel"
flagTs := "ts"
Expand All @@ -135,12 +161,24 @@ You can get the channel ID and the timestamp from the URL of the thread.`,
return fetch(s)
},
}

cmd.Flags().String(flagToken, "", "The Slack token.")
cmd.MarkFlagRequired(flagToken)
if tokenConfig := c.GetString("slack.token"); tokenConfig != "" {
cmd.Flags().Set(flagToken, tokenConfig)
}

cmd.Flags().String(flagChannel, "", "The channel ID of the Slack thread.")
cmd.MarkFlagRequired(flagChannel)
if channelConfig := c.GetString("slack.channel"); channelConfig != "" {
cmd.Flags().Set(flagChannel, channelConfig)
}

cmd.Flags().String(flagTs, "", "The timestamp of the Slack thread.")
cmd.MarkFlagRequired(flagTs)
if tsConfig := c.GetString("slack.ts"); tsConfig != "" {
cmd.Flags().Set(flagTs, tsConfig)
}

return cmd
}
Expand All @@ -150,7 +188,7 @@ const (
aiFlagPrompt string = "prompt"
)

func ollamaCmd() *cobra.Command {
func ollamaCmd(c *config) *cobra.Command {
flagUrl := "url"
var cmd = &cobra.Command{
Use: "ollama",
Expand All @@ -174,13 +212,26 @@ summaraizer ollama --model llama3.2:3b`,
return summarize(p)
},
}

cmd.Flags().String(aiFlagModel, "gemma:2b", "The AI model to use")
if modelConfig := c.GetString("ollama.model"); modelConfig != "" {
cmd.Flags().Set(aiFlagModel, modelConfig)
}

cmd.Flags().String(aiFlagPrompt, defaultPromptTemplate, "The prompt to use for the AI model")
if promptConfig := c.GetString("ollama.prompt"); promptConfig != "" {
cmd.Flags().Set(aiFlagPrompt, promptConfig)
}

cmd.Flags().String(flagUrl, "http://localhost:11434", "The URl where ollama is accessible")
if urlConfig := c.GetString("ollama.url"); urlConfig != "" {
cmd.Flags().Set(flagUrl, urlConfig)
}

return cmd
}

func openaiCmd() *cobra.Command {
func openaiCmd(c *config) *cobra.Command {
flagToken := "token"
cmd := &cobra.Command{
Use: "openai",
Expand All @@ -203,14 +254,27 @@ summaraizer openai --token <token> --model gpt4o-mini`,
return summarize(p)
},
}

cmd.Flags().String(aiFlagModel, "gpt-3.5-turbo", "The AI model to use")
if modelConfig := c.GetString("openai.model"); modelConfig != "" {
cmd.Flags().Set(aiFlagModel, modelConfig)
}

cmd.Flags().String(aiFlagPrompt, defaultPromptTemplate, "The prompt to use for the AI model")
if promptConfig := c.GetString("openai.prompt"); promptConfig != "" {
cmd.Flags().Set(aiFlagPrompt, promptConfig)
}

cmd.Flags().String(flagToken, "", "The API Token for OpenAI")
cmd.MarkFlagRequired(flagToken)
if tokenConfig := c.GetString("openai.token"); tokenConfig != "" {
cmd.Flags().Set(flagToken, tokenConfig)
}

return cmd
}

func anthropicCmd() *cobra.Command {
func anthropicCmd(c *config) *cobra.Command {
flagToken := "token"
cmd := &cobra.Command{
Use: "anthropic",
Expand All @@ -233,14 +297,27 @@ summaraizer anthropic --token <token> --model claude-3-5-sonnet-20241022`,
return summarize(p)
},
}

cmd.Flags().String(aiFlagModel, "claude-3-haiku-20240307", "The AI model to use")
if modelConfig := c.GetString("anthropic.model"); modelConfig != "" {
cmd.Flags().Set(aiFlagModel, modelConfig)
}

cmd.Flags().String(aiFlagPrompt, defaultPromptTemplate, "The prompt to use for the AI model")
if promptConfig := c.GetString("anthropic.prompt"); promptConfig != "" {
cmd.Flags().Set(aiFlagPrompt, promptConfig)
}

cmd.Flags().String(flagToken, "", "The API Token for Anthropic")
cmd.MarkFlagRequired(flagToken)
if tokenConfig := c.GetString("anthropic.token"); tokenConfig != "" {
cmd.Flags().Set(flagToken, tokenConfig)
}

return cmd
}

func googleCmd() *cobra.Command {
func googleCmd(c *config) *cobra.Command {
flagToken := "token"
cmd := &cobra.Command{
Use: "google",
Expand All @@ -263,10 +340,23 @@ summaraizer google --token <token> --model gemini-1.5-pro-002`,
return summarize(p)
},
}

cmd.Flags().String(aiFlagModel, "gemini-1.5-flash-8b", "The AI model to use")
if modelConfig := c.GetString("google.model"); modelConfig != "" {
cmd.Flags().Set(aiFlagModel, modelConfig)
}

cmd.Flags().String(aiFlagPrompt, defaultPromptTemplate, "The prompt to use for the AI model")
if promptConfig := c.GetString("google.prompt"); promptConfig != "" {
cmd.Flags().Set(aiFlagPrompt, promptConfig)
}

cmd.Flags().String(flagToken, "", "The API Token for Google")
cmd.MarkFlagRequired(flagToken)
if tokenConfig := c.GetString("google.token"); tokenConfig != "" {
cmd.Flags().Set(flagToken, tokenConfig)
}

return cmd
}

Expand Down
33 changes: 33 additions & 0 deletions internal/cli/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cli

import (
"os"
"strings"

"github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
)

type config struct {
k *koanf.Koanf
}

func newConfig() *config {
k := koanf.New(".")
k.Load(file.Provider("config.json"), json.Parser())
k.Load(file.Provider(os.ExpandEnv("$HOME/.config/summaraizer/config.json")), json.Parser())
k.Load(file.Provider(os.ExpandEnv("$HOME/.summaraizer/config.json")), json.Parser())
k.Load(env.Provider("SUMMARAIZER_", ".", func(s string) string {
envWithoutPrefix := strings.TrimPrefix(s, "SUMMARAIZER_")
return strings.Replace(strings.ToLower(envWithoutPrefix), "_", ".", -1)
}), nil)
return &config{
k: k,
}
}

func (c *config) GetString(path string) string {
return c.k.String(path)
}
Loading