Skip to content

Commit

Permalink
fix: linux docker url parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
sammcj committed Jun 20, 2024
1 parent a8ed5e1 commit d4cb3e9
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ node_modules/
.idea/
.vscode/
Modelfile*
**/*.DS_Store
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ install: ## Install latest
go install github.com/sammcj/gollama@latest

run: ## Run
go run $(shell find . -type f -depth 1 -name '*.go' -not -name '*_test.go')
@go run $(shell find *.go -not -name '*_test.go')
15 changes: 4 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var defaultConfig = Config{
DefaultSort: "Size",
Columns: []string{"Name", "Size", "Quant", "Family", "Modified", "ID"},
OllamaAPIKey: "",
OllamaAPIURL: "http://localhost:11434",
OllamaAPIURL: os.Getenv("OLLAMA_HOST"),
LMStudioFilePaths: "",
LogLevel: "info",
LogFilePath: os.Getenv("HOME") + "/.config/gollama/gollama.log",
Expand Down Expand Up @@ -83,19 +83,12 @@ func LoadConfig() (Config, error) {
return Config{}, fmt.Errorf("failed to decode config file: %w", err)
}

// Set OLLAMA_HOST if OllamaAPIURL is set and valid
if config.OllamaAPIURL != "" {
os.Setenv("OLLAMA_HOST", config.OllamaAPIURL)
}

// If ollama_api_url is not set and OLLAMA_HOST is set, use OLLAMA_HOST
if config.OllamaAPIURL == "" {
config.OllamaAPIURL = os.Getenv("OLLAMA_HOST")
}

// Set the last sort selection to the current sort order
config.LastSortSelection = config.SortOrder

if config.LogLevel == "debug" {
logging.DebugLogger.Println("Config loaded:", config)
}
return config, nil
}

Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/harmonica v0.2.0 // indirect
github.com/charmbracelet/x/ansi v0.1.2 // indirect
github.com/charmbracelet/x/input v0.1.0 // indirect
github.com/charmbracelet/x/input v0.1.2 // indirect
github.com/charmbracelet/x/term v0.1.1 // indirect
github.com/charmbracelet/x/windows v0.1.0 // indirect
github.com/charmbracelet/x/windows v0.1.2 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand All @@ -32,11 +32,11 @@ require (
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
github.com/sahilm/fuzzy v0.1.1 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ github.com/charmbracelet/lipgloss v0.11.0 h1:UoAcbQ6Qml8hDwSWs0Y1cB5TEQuZkDPH/Zq
github.com/charmbracelet/lipgloss v0.11.0/go.mod h1:1UdRTH9gYgpcdNN5oBtjbu/IzNKtzVtb7sqN1t9LNn8=
github.com/charmbracelet/x/ansi v0.1.2 h1:6+LR39uG8DE6zAmbu023YlqjJHkYXDF1z36ZwzO4xZY=
github.com/charmbracelet/x/ansi v0.1.2/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=
github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=
github.com/charmbracelet/x/input v0.1.2 h1:QJAZr33eOhDowkkEQ24rsJy4Llxlm+fRDf/cQrmqJa0=
github.com/charmbracelet/x/input v0.1.2/go.mod h1:LGBim0maUY4Pitjn/4fHnuXb4KirU3DODsyuHuXdOyA=
github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=
github.com/charmbracelet/x/term v0.1.1/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw=
github.com/charmbracelet/x/windows v0.1.0 h1:gTaxdvzDM5oMa/I2ZNF7wN78X/atWemG9Wph7Ika2k4=
github.com/charmbracelet/x/windows v0.1.0/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ=
github.com/charmbracelet/x/windows v0.1.2 h1:Iumiwq2G+BRmgoayww/qfcvof7W/3uLoelhxojXlRWg=
github.com/charmbracelet/x/windows v0.1.2/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
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=
Expand Down Expand Up @@ -63,8 +63,8 @@ github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA=
github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
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/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
Expand All @@ -81,8 +81,8 @@ golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
Expand Down
3 changes: 3 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
)

func parseAPIResponse(resp *api.ListResponse) []Model {
logging.DebugLogger.Println("Fetching models from API")

models := make([]Model, len(resp.Models))
for i, modelResp := range resp.Models {
modelName := lipgloss.NewStyle().Foreground(lipgloss.Color("white")).Render(modelResp.Name)
Expand All @@ -27,6 +29,7 @@ func parseAPIResponse(resp *api.ListResponse) []Model {
Modified: modelResp.ModifiedAt,
}
}
logging.DebugLogger.Println("Models:", models)
return models
}

Expand Down
8 changes: 6 additions & 2 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func Init(logLevel, logFilePath string) error {
// Configure log rotation with lumberjack
rotate := &lumberjack.Logger{
Filename: logFilePath,
MaxSize: 10, // megabytes
MaxSize: 2, // megabytes
MaxBackups: 3, // number of files
MaxAge: 28, // days
MaxAge: 60, // days
Compress: false, // disabled by default
}

Expand All @@ -65,5 +65,9 @@ func Init(logLevel, logFilePath string) error {
InfoLogger = log.Logger.Level(zerolog.InfoLevel)
ErrorLogger = log.Logger.Level(zerolog.ErrorLevel)

if logLevel == "debug" {
DebugLogger.Printf("Logging to: %s\n", logFilePath)
}

return nil
}
3 changes: 1 addition & 2 deletions logging/logging_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package logging

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -95,7 +94,7 @@ func TestInit(t *testing.T) {
DebugLogger.Debug().Msg("This is a debug message")

// Check the contents of the log file
data, err := ioutil.ReadFile(actualLogFilePath)
data, err := os.ReadFile(actualLogFilePath)
if err != nil {
t.Fatalf("Failed to read log file: %v", err)
}
Expand Down
21 changes: 11 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"context"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -98,22 +100,21 @@ func main() {
}

os.Setenv("EDITOR", cfg.Editor)
logging.DebugLogger.Println("EDITOR set to", cfg.Editor)

client, err := api.ClientFromEnvironment()
if err != nil {
logging.ErrorLogger.Println("Error creating API client:", err)
return
}

// Initialize the API client
ctx := context.Background()
httpClient := &http.Client{}
url, err := url.Parse(cfg.OllamaAPIURL)
client := api.NewClient(url, httpClient)

resp, err := client.List(ctx)
if err != nil {
logging.ErrorLogger.Println("Error fetching models:", err)
return
message := fmt.Sprintf("Error fetching models:\n- Error: %v\n- Configured API URL: %v", err, cfg.OllamaAPIURL)
logging.ErrorLogger.Println(message)
fmt.Println(message)
os.Exit(1)
}

logging.InfoLogger.Println("Fetched models from API")
models := parseAPIResponse(resp)

modelMap := make(map[string][]Model)
Expand Down

0 comments on commit d4cb3e9

Please sign in to comment.