From be7709a5edcb3931564bf11ad9072c003fd3d4df Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Thu, 21 Sep 2023 16:18:18 -0700 Subject: [PATCH] Move some things out of internal --- Makefile | 10 ++--- build.go | 8 ++-- cmd/lure-api-server/api.go | 4 +- cmd/lure-api-server/badge.go | 2 +- cmd/lure-api-server/main.go | 4 +- cmd/lure-api-server/webhook.go | 4 +- fix.go | 6 +-- info.go | 6 +-- install.go | 10 ++--- internal/api/lure.pb.go | 37 ++++++++++--------- internal/api/lure.twirp.go | 36 ++++++++++-------- internal/cliutils/prompt.go | 4 +- internal/config/version.go | 26 ------------- internal/dlcache/dlcache.go | 2 +- internal/dlcache/dlcache_test.go | 2 +- internal/overrides/overrides.go | 4 +- internal/overrides/overrides_test.go | 2 +- internal/shutils/decoder/decoder.go | 2 +- internal/shutils/decoder/decoder_test.go | 2 +- internal/shutils/exec_test.go | 2 +- internal/types/build.go | 2 +- list.go | 8 ++-- main.go | 6 +-- {internal => pkg}/build/build.go | 10 ++--- {internal => pkg}/build/install.go | 12 +++--- {internal => pkg}/config/config.go | 3 ++ {internal => pkg}/config/lang.go | 6 +++ .../config/dirs.go => pkg/config/paths.go | 5 +++ pkg/config/version.go | 5 +++ {internal => pkg}/db/db.go | 32 +++++++++++----- {internal => pkg}/db/db_test.go | 2 +- {distro => pkg/distro}/osrelease.go | 9 ++--- {manager => pkg/manager}/apk.go | 0 {manager => pkg/manager}/apt.go | 0 {manager => pkg/manager}/dnf.go | 0 {manager => pkg/manager}/managers.go | 0 {manager => pkg/manager}/pacman.go | 0 {manager => pkg/manager}/yum.go | 0 {manager => pkg/manager}/zypper.go | 0 {internal => pkg}/repos/find.go | 4 +- {internal => pkg}/repos/find_test.go | 4 +- {internal => pkg}/repos/pull.go | 16 ++++---- {internal => pkg}/repos/pull_test.go | 6 +-- repo.go | 6 +-- scripts/gen-version.sh | 3 -- upgrade.go | 12 +++--- 46 files changed, 168 insertions(+), 156 deletions(-) delete mode 100644 internal/config/version.go rename {internal => pkg}/build/build.go (99%) rename {internal => pkg}/build/install.go (85%) rename {internal => pkg}/config/config.go (91%) rename {internal => pkg}/config/lang.go (82%) rename internal/config/dirs.go => pkg/config/paths.go (91%) create mode 100644 pkg/config/version.go rename {internal => pkg}/db/db.go (87%) rename {internal => pkg}/db/db_test.go (99%) rename {distro => pkg/distro}/osrelease.go (94%) rename {manager => pkg/manager}/apk.go (100%) rename {manager => pkg/manager}/apt.go (100%) rename {manager => pkg/manager}/dnf.go (100%) rename {manager => pkg/manager}/managers.go (100%) rename {manager => pkg/manager}/pacman.go (100%) rename {manager => pkg/manager}/yum.go (100%) rename {manager => pkg/manager}/zypper.go (100%) rename {internal => pkg}/repos/find.go (94%) rename {internal => pkg}/repos/find_test.go (98%) rename {internal => pkg}/repos/pull.go (95%) rename {internal => pkg}/repos/pull_test.go (95%) delete mode 100755 scripts/gen-version.sh diff --git a/Makefile b/Makefile index 8148dd9..2c2e0ac 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,8 @@ PREFIX ?= /usr/local +GIT_VERSION = $(shell git describe --tags ) -lure: internal/config/version.txt - CGO_ENABLED=0 go build +lure: + CGO_ENABLED=0 go build -ldflags="-X 'go.elara.ws/lure/pkg/config.Version=$(GIT_VERSION)'" clean: rm -f lure @@ -15,8 +16,5 @@ installmisc: uninstall: rm -f /usr/local/bin/lure - -internal/config/version.txt: - go generate ./internal/config -.PHONY: install clean uninstall installmisc \ No newline at end of file +.PHONY: install clean uninstall installmisc lure \ No newline at end of file diff --git a/build.go b/build.go index 586207d..4ca76b4 100644 --- a/build.go +++ b/build.go @@ -23,13 +23,13 @@ import ( "path/filepath" "github.com/urfave/cli/v2" - "go.elara.ws/lure/internal/build" - "go.elara.ws/lure/internal/config" "go.elara.ws/lure/internal/log" "go.elara.ws/lure/internal/osutils" - "go.elara.ws/lure/internal/repos" "go.elara.ws/lure/internal/types" - "go.elara.ws/lure/manager" + "go.elara.ws/lure/pkg/build" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/manager" + "go.elara.ws/lure/pkg/repos" ) var buildCmd = &cli.Command{ diff --git a/cmd/lure-api-server/api.go b/cmd/lure-api-server/api.go index 5a06ac0..bcceec4 100644 --- a/cmd/lure-api-server/api.go +++ b/cmd/lure-api-server/api.go @@ -27,9 +27,9 @@ import ( "github.com/twitchtv/twirp" "go.elara.ws/lure/internal/api" - "go.elara.ws/lure/internal/config" - "go.elara.ws/lure/internal/db" "go.elara.ws/lure/internal/log" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/db" "golang.org/x/text/language" ) diff --git a/cmd/lure-api-server/badge.go b/cmd/lure-api-server/badge.go index aa08761..8af61e9 100644 --- a/cmd/lure-api-server/badge.go +++ b/cmd/lure-api-server/badge.go @@ -8,7 +8,7 @@ import ( "strings" "github.com/go-chi/chi/v5" - "go.elara.ws/lure/internal/db" + "go.elara.ws/lure/pkg/db" ) //go:embed badge-logo.txt diff --git a/cmd/lure-api-server/main.go b/cmd/lure-api-server/main.go index fc7d441..75c26f1 100644 --- a/cmd/lure-api-server/main.go +++ b/cmd/lure-api-server/main.go @@ -29,9 +29,9 @@ import ( "github.com/twitchtv/twirp" "go.elara.ws/logger" "go.elara.ws/lure/internal/api" - "go.elara.ws/lure/internal/config" "go.elara.ws/lure/internal/log" - "go.elara.ws/lure/internal/repos" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/repos" ) func init() { diff --git a/cmd/lure-api-server/webhook.go b/cmd/lure-api-server/webhook.go index 0432aa1..3faf5b5 100644 --- a/cmd/lure-api-server/webhook.go +++ b/cmd/lure-api-server/webhook.go @@ -29,9 +29,9 @@ import ( "os" "strings" - "go.elara.ws/lure/internal/config" "go.elara.ws/lure/internal/log" - "go.elara.ws/lure/internal/repos" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/repos" ) func handleWebhook(sigCh chan<- struct{}) http.HandlerFunc { diff --git a/fix.go b/fix.go index 607a6da..0de54ac 100644 --- a/fix.go +++ b/fix.go @@ -22,10 +22,10 @@ import ( "os" "github.com/urfave/cli/v2" - "go.elara.ws/lure/internal/config" - "go.elara.ws/lure/internal/db" "go.elara.ws/lure/internal/log" - "go.elara.ws/lure/internal/repos" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/db" + "go.elara.ws/lure/pkg/repos" ) var fixCmd = &cli.Command{ diff --git a/info.go b/info.go index 4012fe3..ff331bc 100644 --- a/info.go +++ b/info.go @@ -25,11 +25,11 @@ import ( "go.elara.ws/lure/internal/log" "github.com/urfave/cli/v2" - "go.elara.ws/lure/distro" "go.elara.ws/lure/internal/cliutils" - "go.elara.ws/lure/internal/config" "go.elara.ws/lure/internal/overrides" - "go.elara.ws/lure/internal/repos" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/distro" + "go.elara.ws/lure/pkg/repos" "gopkg.in/yaml.v3" ) diff --git a/install.go b/install.go index 55ca9e8..f85dda2 100644 --- a/install.go +++ b/install.go @@ -22,14 +22,14 @@ import ( "fmt" "github.com/urfave/cli/v2" - "go.elara.ws/lure/internal/build" "go.elara.ws/lure/internal/cliutils" - "go.elara.ws/lure/internal/config" - "go.elara.ws/lure/internal/db" "go.elara.ws/lure/internal/log" - "go.elara.ws/lure/internal/repos" "go.elara.ws/lure/internal/types" - "go.elara.ws/lure/manager" + "go.elara.ws/lure/pkg/build" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/db" + "go.elara.ws/lure/pkg/manager" + "go.elara.ws/lure/pkg/repos" ) var installCmd = &cli.Command{ diff --git a/internal/api/lure.pb.go b/internal/api/lure.pb.go index 2f8bde3..c2e43a9 100644 --- a/internal/api/lure.pb.go +++ b/internal/api/lure.pb.go @@ -7,10 +7,11 @@ package api import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" ) const ( @@ -734,21 +735,23 @@ func file_lure_proto_rawDescGZIP() []byte { return file_lure_proto_rawDescData } -var file_lure_proto_enumTypes = make([]protoimpl.EnumInfo, 2) -var file_lure_proto_msgTypes = make([]protoimpl.MessageInfo, 9) -var file_lure_proto_goTypes = []interface{}{ - (SORT_BY)(0), // 0: lure.SORT_BY - (FILTER_TYPE)(0), // 1: lure.FILTER_TYPE - (*SearchRequest)(nil), // 2: lure.SearchRequest - (*StringList)(nil), // 3: lure.StringList - (*Package)(nil), // 4: lure.Package - (*GetPackageRequest)(nil), // 5: lure.GetPackageRequest - (*SearchResponse)(nil), // 6: lure.SearchResponse - (*GetBuildScriptRequest)(nil), // 7: lure.GetBuildScriptRequest - (*GetBuildScriptResponse)(nil), // 8: lure.GetBuildScriptResponse - nil, // 9: lure.Package.DependsEntry - nil, // 10: lure.Package.BuildDependsEntry -} +var ( + file_lure_proto_enumTypes = make([]protoimpl.EnumInfo, 2) + file_lure_proto_msgTypes = make([]protoimpl.MessageInfo, 9) + file_lure_proto_goTypes = []interface{}{ + (SORT_BY)(0), // 0: lure.SORT_BY + (FILTER_TYPE)(0), // 1: lure.FILTER_TYPE + (*SearchRequest)(nil), // 2: lure.SearchRequest + (*StringList)(nil), // 3: lure.StringList + (*Package)(nil), // 4: lure.Package + (*GetPackageRequest)(nil), // 5: lure.GetPackageRequest + (*SearchResponse)(nil), // 6: lure.SearchResponse + (*GetBuildScriptRequest)(nil), // 7: lure.GetBuildScriptRequest + (*GetBuildScriptResponse)(nil), // 8: lure.GetBuildScriptResponse + nil, // 9: lure.Package.DependsEntry + nil, // 10: lure.Package.BuildDependsEntry + } +) var file_lure_proto_depIdxs = []int32{ 0, // 0: lure.SearchRequest.sort_by:type_name -> lure.SORT_BY 1, // 1: lure.SearchRequest.filter_type:type_name -> lure.FILTER_TYPE diff --git a/internal/api/lure.twirp.go b/internal/api/lure.twirp.go index dc8c799..c2e1927 100644 --- a/internal/api/lure.twirp.go +++ b/internal/api/lure.twirp.go @@ -3,23 +3,29 @@ package api -import context "context" -import fmt "fmt" -import http "net/http" -import io "io" -import json "encoding/json" -import strconv "strconv" -import strings "strings" +import ( + context "context" + fmt "fmt" + http "net/http" + io "io" + json "encoding/json" + strconv "strconv" + strings "strings" +) -import protojson "google.golang.org/protobuf/encoding/protojson" -import proto "google.golang.org/protobuf/proto" -import twirp "github.com/twitchtv/twirp" -import ctxsetters "github.com/twitchtv/twirp/ctxsetters" +import ( + protojson "google.golang.org/protobuf/encoding/protojson" + proto "google.golang.org/protobuf/proto" + twirp "github.com/twitchtv/twirp" + ctxsetters "github.com/twitchtv/twirp/ctxsetters" +) -import bytes "bytes" -import errors "errors" -import path "path" -import url "net/url" +import ( + bytes "bytes" + errors "errors" + path "path" + url "net/url" +) // Version compatibility assertion. // If the constant is not defined in the package, that likely means diff --git a/internal/cliutils/prompt.go b/internal/cliutils/prompt.go index 03905f0..899d541 100644 --- a/internal/cliutils/prompt.go +++ b/internal/cliutils/prompt.go @@ -23,11 +23,11 @@ import ( "strings" "github.com/AlecAivazis/survey/v2" - "go.elara.ws/lure/internal/config" - "go.elara.ws/lure/internal/db" "go.elara.ws/lure/internal/log" "go.elara.ws/lure/internal/pager" "go.elara.ws/lure/internal/translations" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/db" ) // YesNoPrompt asks the user a yes or no question, using def as the default answer diff --git a/internal/config/version.go b/internal/config/version.go deleted file mode 100644 index 622d782..0000000 --- a/internal/config/version.go +++ /dev/null @@ -1,26 +0,0 @@ -/* - * LURE - Linux User REpository - * Copyright (C) 2023 Elara Musayelyan - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package config - -import _ "embed" - -//go:generate ../../scripts/gen-version.sh - -//go:embed version.txt -var Version string diff --git a/internal/dlcache/dlcache.go b/internal/dlcache/dlcache.go index bc3cbfe..8fc6fd2 100644 --- a/internal/dlcache/dlcache.go +++ b/internal/dlcache/dlcache.go @@ -25,7 +25,7 @@ import ( "os" "path/filepath" - "go.elara.ws/lure/internal/config" + "go.elara.ws/lure/pkg/config" ) // BasePath returns the base path of the download cache diff --git a/internal/dlcache/dlcache_test.go b/internal/dlcache/dlcache_test.go index 9ac5724..257c1d4 100644 --- a/internal/dlcache/dlcache_test.go +++ b/internal/dlcache/dlcache_test.go @@ -26,8 +26,8 @@ import ( "path/filepath" "testing" - "go.elara.ws/lure/internal/config" "go.elara.ws/lure/internal/dlcache" + "go.elara.ws/lure/pkg/config" ) func init() { diff --git a/internal/overrides/overrides.go b/internal/overrides/overrides.go index 759324d..44351d6 100644 --- a/internal/overrides/overrides.go +++ b/internal/overrides/overrides.go @@ -22,9 +22,9 @@ import ( "reflect" "strings" - "go.elara.ws/lure/distro" "go.elara.ws/lure/internal/cpu" - "go.elara.ws/lure/internal/db" + "go.elara.ws/lure/pkg/db" + "go.elara.ws/lure/pkg/distro" "golang.org/x/exp/slices" "golang.org/x/text/language" ) diff --git a/internal/overrides/overrides_test.go b/internal/overrides/overrides_test.go index ab6249a..ec69fe0 100644 --- a/internal/overrides/overrides_test.go +++ b/internal/overrides/overrides_test.go @@ -23,8 +23,8 @@ import ( "reflect" "testing" - "go.elara.ws/lure/distro" "go.elara.ws/lure/internal/overrides" + "go.elara.ws/lure/pkg/distro" "golang.org/x/text/language" ) diff --git a/internal/shutils/decoder/decoder.go b/internal/shutils/decoder/decoder.go index 1f590f6..49299e7 100644 --- a/internal/shutils/decoder/decoder.go +++ b/internal/shutils/decoder/decoder.go @@ -25,8 +25,8 @@ import ( "strings" "github.com/mitchellh/mapstructure" - "go.elara.ws/lure/distro" "go.elara.ws/lure/internal/overrides" + "go.elara.ws/lure/pkg/distro" "golang.org/x/exp/slices" "mvdan.cc/sh/v3/expand" "mvdan.cc/sh/v3/interp" diff --git a/internal/shutils/decoder/decoder_test.go b/internal/shutils/decoder/decoder_test.go index a143c92..79e4a8b 100644 --- a/internal/shutils/decoder/decoder_test.go +++ b/internal/shutils/decoder/decoder_test.go @@ -27,8 +27,8 @@ import ( "strings" "testing" - "go.elara.ws/lure/distro" "go.elara.ws/lure/internal/shutils/decoder" + "go.elara.ws/lure/pkg/distro" "mvdan.cc/sh/v3/interp" "mvdan.cc/sh/v3/syntax" ) diff --git a/internal/shutils/exec_test.go b/internal/shutils/exec_test.go index 23a7d17..892edb5 100644 --- a/internal/shutils/exec_test.go +++ b/internal/shutils/exec_test.go @@ -23,9 +23,9 @@ import ( "strings" "testing" - "go.elara.ws/lure/distro" "go.elara.ws/lure/internal/shutils" "go.elara.ws/lure/internal/shutils/decoder" + "go.elara.ws/lure/pkg/distro" "mvdan.cc/sh/v3/interp" "mvdan.cc/sh/v3/syntax" ) diff --git a/internal/types/build.go b/internal/types/build.go index 82667dc..d81ceec 100644 --- a/internal/types/build.go +++ b/internal/types/build.go @@ -18,7 +18,7 @@ package types -import "go.elara.ws/lure/manager" +import "go.elara.ws/lure/pkg/manager" type BuildOpts struct { Script string diff --git a/list.go b/list.go index 687df79..68da552 100644 --- a/list.go +++ b/list.go @@ -22,11 +22,11 @@ import ( "fmt" "github.com/urfave/cli/v2" - "go.elara.ws/lure/internal/config" - "go.elara.ws/lure/internal/db" "go.elara.ws/lure/internal/log" - "go.elara.ws/lure/internal/repos" - "go.elara.ws/lure/manager" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/db" + "go.elara.ws/lure/pkg/manager" + "go.elara.ws/lure/pkg/repos" "golang.org/x/exp/slices" ) diff --git a/main.go b/main.go index 4a55bc0..0c998ea 100644 --- a/main.go +++ b/main.go @@ -28,11 +28,11 @@ import ( "github.com/mattn/go-isatty" "github.com/urfave/cli/v2" "go.elara.ws/logger" - "go.elara.ws/lure/internal/config" - "go.elara.ws/lure/internal/db" "go.elara.ws/lure/internal/log" "go.elara.ws/lure/internal/translations" - "go.elara.ws/lure/manager" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/db" + "go.elara.ws/lure/pkg/manager" ) var app = &cli.App{ diff --git a/internal/build/build.go b/pkg/build/build.go similarity index 99% rename from internal/build/build.go rename to pkg/build/build.go index f8b4345..6030c46 100644 --- a/internal/build/build.go +++ b/pkg/build/build.go @@ -38,19 +38,19 @@ import ( "github.com/goreleaser/nfpm/v2" "github.com/goreleaser/nfpm/v2/files" - "go.elara.ws/lure/distro" "go.elara.ws/lure/internal/cliutils" - "go.elara.ws/lure/internal/config" "go.elara.ws/lure/internal/cpu" - "go.elara.ws/lure/internal/db" "go.elara.ws/lure/internal/dl" "go.elara.ws/lure/internal/log" - "go.elara.ws/lure/internal/repos" "go.elara.ws/lure/internal/shutils" "go.elara.ws/lure/internal/shutils/decoder" "go.elara.ws/lure/internal/shutils/helpers" "go.elara.ws/lure/internal/types" - "go.elara.ws/lure/manager" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/db" + "go.elara.ws/lure/pkg/distro" + "go.elara.ws/lure/pkg/manager" + "go.elara.ws/lure/pkg/repos" "mvdan.cc/sh/v3/expand" "mvdan.cc/sh/v3/interp" "mvdan.cc/sh/v3/syntax" diff --git a/internal/build/install.go b/pkg/build/install.go similarity index 85% rename from internal/build/install.go rename to pkg/build/install.go index ed81095..8d10e49 100644 --- a/internal/build/install.go +++ b/pkg/build/install.go @@ -22,14 +22,14 @@ import ( "context" "path/filepath" - "go.elara.ws/lure/internal/config" - "go.elara.ws/lure/internal/db" "go.elara.ws/lure/internal/log" "go.elara.ws/lure/internal/types" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/db" ) -// InstallPkgs installs non-LURE packages via the package manager, then builds and installs LURE -// packages +// InstallPkgs installs native packages via the package manager, +// then builds and installs the LURE packages func InstallPkgs(ctx context.Context, lurePkgs []db.Package, nativePkgs []string, opts types.BuildOpts) { if len(nativePkgs) > 0 { err := opts.Manager.Install(nil, nativePkgs...) @@ -41,7 +41,7 @@ func InstallPkgs(ctx context.Context, lurePkgs []db.Package, nativePkgs []string InstallScripts(ctx, GetScriptPaths(lurePkgs), opts) } -// GetScriptPaths generates a slice of script paths corresponding to the +// GetScriptPaths returns a slice of script paths corresponding to the // given packages func GetScriptPaths(pkgs []db.Package) []string { var scripts []string @@ -52,7 +52,7 @@ func GetScriptPaths(pkgs []db.Package) []string { return scripts } -// InstallScripts builds and installs LURE build scripts +// InstallScripts builds and installs the given LURE build scripts func InstallScripts(ctx context.Context, scripts []string, opts types.BuildOpts) { for _, script := range scripts { opts.Script = script diff --git a/internal/config/config.go b/pkg/config/config.go similarity index 91% rename from internal/config/config.go rename to pkg/config/config.go index 38b27af..53ee8e4 100644 --- a/internal/config/config.go +++ b/pkg/config/config.go @@ -40,6 +40,9 @@ var defaultConfig = &types.Config{ var config *types.Config +// Config returns a LURE configuration struct. +// The first time it's called, it'll load the config from a file. +// Subsequent calls will just return the same value. func Config() *types.Config { if config == nil { cfgFl, err := os.Open(GetPaths().ConfigPath) diff --git a/internal/config/lang.go b/pkg/config/lang.go similarity index 82% rename from internal/config/lang.go rename to pkg/config/lang.go index 3364d55..d8a0639 100644 --- a/internal/config/lang.go +++ b/pkg/config/lang.go @@ -31,6 +31,10 @@ var ( langSet bool ) +// Language returns the system language. +// The first time it's called, it'll detect the langauge based on +// the $LANG environment variable. +// Subsequent calls will just return the same value. func Language() language.Tag { if !langSet { syslang := SystemLang() @@ -45,6 +49,8 @@ func Language() language.Tag { return lang } +// SystemLang returns the system language based on +// the $LANG environment variable. func SystemLang() string { lang := os.Getenv("LANG") lang, _, _ = strings.Cut(lang, ".") diff --git a/internal/config/dirs.go b/pkg/config/paths.go similarity index 91% rename from internal/config/dirs.go rename to pkg/config/paths.go index 331ae93..909efbc 100644 --- a/internal/config/dirs.go +++ b/pkg/config/paths.go @@ -26,6 +26,7 @@ import ( "go.elara.ws/lure/internal/log" ) +// Paths contains various paths used by LURE type Paths struct { ConfigDir string ConfigPath string @@ -37,6 +38,10 @@ type Paths struct { var paths *Paths +// GetPaths returns a Paths struct. +// The first time it's called, it'll generate the struct +// using information from the system. +// Subsequent calls will return the same value. func GetPaths() *Paths { if paths == nil { paths = &Paths{} diff --git a/pkg/config/version.go b/pkg/config/version.go new file mode 100644 index 0000000..33cf91a --- /dev/null +++ b/pkg/config/version.go @@ -0,0 +1,5 @@ +package config + +// Version contains the version of LURE. If the version +// isn't known, it'll be set to "unknown" +var Version = "unknown" diff --git a/internal/db/db.go b/pkg/db/db.go similarity index 87% rename from internal/db/db.go rename to pkg/db/db.go index a19c0a6..339516e 100644 --- a/internal/db/db.go +++ b/pkg/db/db.go @@ -26,16 +26,18 @@ import ( "fmt" "github.com/jmoiron/sqlx" - "go.elara.ws/lure/internal/config" "go.elara.ws/lure/internal/log" + "go.elara.ws/lure/pkg/config" "golang.org/x/exp/slices" "modernc.org/sqlite" ) +// CurrentVersion is the current version of the database. +// The database is reset if its version doesn't match this. const CurrentVersion = 2 func init() { - sqlite.MustRegisterScalarFunction("json_array_contains", 2, JsonArrayContains) + sqlite.MustRegisterScalarFunction("json_array_contains", 2, jsonArrayContains) } // Package is a LURE package's database representation @@ -67,11 +69,14 @@ var ( closed = true ) +// DB returns the LURE database. +// The first time it's called, it opens the SQLite database file. +// Subsequent calls return the same connection. func DB() *sqlx.DB { if conn != nil && !closed { return conn } - db, err := Open(config.GetPaths().DBPath) + db, err := open(config.GetPaths().DBPath) if err != nil { log.Fatal("Error opening database").Err(err).Send() } @@ -79,7 +84,7 @@ func DB() *sqlx.DB { return conn } -func Open(dsn string) (*sqlx.DB, error) { +func open(dsn string) (*sqlx.DB, error) { db, err := sqlx.Open("sqlite", dsn) if err != nil { return nil, err @@ -95,6 +100,7 @@ func Open(dsn string) (*sqlx.DB, error) { return db, nil } +// Close closes the database func Close() error { closed = true if conn != nil { @@ -104,7 +110,7 @@ func Close() error { } } -// Init initializes the database +// initDB initializes the database func initDB(dsn string) error { conn = conn.Unsafe() _, err := conn.Exec(` @@ -139,7 +145,7 @@ func initDB(dsn string) error { ver, ok := GetVersion() if ok && ver != CurrentVersion { log.Warn("Database version mismatch; resetting").Int("version", ver).Int("expected", CurrentVersion).Send() - Reset() + reset() return initDB(dsn) } else if !ok { log.Warn("Database version does not exist. Run lure fix if something isn't working.").Send() @@ -149,7 +155,8 @@ func initDB(dsn string) error { return nil } -func Reset() error { +// reset drops all the database tables +func reset() error { _, err := DB().Exec("DROP TABLE IF EXISTS pkgs;") if err != nil { return err @@ -158,6 +165,7 @@ func Reset() error { return err } +// IsEmpty returns true if the database has no packages in it, otherwise it returns false. func IsEmpty() bool { var count int err := DB().Get(&count, "SELECT count(1) FROM pkgs;") @@ -167,6 +175,8 @@ func IsEmpty() bool { return count == 0 } +// GetVersion returns the database version and a boolean indicating +// whether the database contained a version number func GetVersion() (int, bool) { var ver version err := DB().Get(&ver, "SELECT * FROM lure_db_version LIMIT 1;") @@ -232,7 +242,7 @@ func GetPkgs(where string, args ...any) (*sqlx.Rows, error) { return stream, nil } -// GetPkg returns a single package that match the where conditions +// GetPkg returns a single package that matches the where conditions func GetPkg(where string, args ...any) (*Package, error) { out := &Package{} err := DB().Get(out, "SELECT * FROM pkgs WHERE "+where+" LIMIT 1", args...) @@ -245,7 +255,9 @@ func DeletePkgs(where string, args ...any) error { return err } -func JsonArrayContains(ctx *sqlite.FunctionContext, args []driver.Value) (driver.Value, error) { +// jsonArrayContains is an SQLite function that checks if a JSON array +// in the database contains a given value +func jsonArrayContains(ctx *sqlite.FunctionContext, args []driver.Value) (driver.Value, error) { value, ok := args[0].(string) if !ok { return nil, errors.New("both arguments to json_array_contains must be strings") @@ -265,10 +277,12 @@ func JsonArrayContains(ctx *sqlite.FunctionContext, args []driver.Value) (driver return slices.Contains(array, item), nil } +// JSON represents a JSON value in the database type JSON[T any] struct { Val T } +// NewJSON creates a new database JSON value func NewJSON[T any](v T) JSON[T] { return JSON[T]{Val: v} } diff --git a/internal/db/db_test.go b/pkg/db/db_test.go similarity index 99% rename from internal/db/db_test.go rename to pkg/db/db_test.go index 1a93282..e8631ea 100644 --- a/internal/db/db_test.go +++ b/pkg/db/db_test.go @@ -24,7 +24,7 @@ import ( "testing" "github.com/jmoiron/sqlx" - "go.elara.ws/lure/internal/db" + "go.elara.ws/lure/pkg/db" ) var testPkg = db.Package{ diff --git a/distro/osrelease.go b/pkg/distro/osrelease.go similarity index 94% rename from distro/osrelease.go rename to pkg/distro/osrelease.go index 943942f..d79a3ca 100644 --- a/distro/osrelease.go +++ b/pkg/distro/osrelease.go @@ -20,7 +20,6 @@ package distro import ( "context" - "errors" "os" "strings" @@ -30,8 +29,7 @@ import ( "mvdan.cc/sh/v3/syntax" ) -var ErrParse = errors.New("could not parse os-release file") - +// OSRelease contains information from an os-release file type OSRelease struct { Name string PrettyName string @@ -50,7 +48,8 @@ var parsed *OSRelease // OSReleaseName returns a struct parsed from the system's os-release // file. It checks /etc/os-release as well as /usr/lib/os-release. -// The returned OSRelease struct is a singleton. +// The first time it's called, it'll parse the os-release file. +// Subsequent calls will return the same value. func ParseOSRelease(ctx context.Context) (*OSRelease, error) { if parsed != nil { return parsed, nil @@ -87,7 +86,7 @@ func ParseOSRelease(ctx context.Context) (*OSRelease, error) { err = runner.Run(ctx, file) if err != nil { - return nil, ErrParse + return nil, err } out := &OSRelease{ diff --git a/manager/apk.go b/pkg/manager/apk.go similarity index 100% rename from manager/apk.go rename to pkg/manager/apk.go diff --git a/manager/apt.go b/pkg/manager/apt.go similarity index 100% rename from manager/apt.go rename to pkg/manager/apt.go diff --git a/manager/dnf.go b/pkg/manager/dnf.go similarity index 100% rename from manager/dnf.go rename to pkg/manager/dnf.go diff --git a/manager/managers.go b/pkg/manager/managers.go similarity index 100% rename from manager/managers.go rename to pkg/manager/managers.go diff --git a/manager/pacman.go b/pkg/manager/pacman.go similarity index 100% rename from manager/pacman.go rename to pkg/manager/pacman.go diff --git a/manager/yum.go b/pkg/manager/yum.go similarity index 100% rename from manager/yum.go rename to pkg/manager/yum.go diff --git a/manager/zypper.go b/pkg/manager/zypper.go similarity index 100% rename from manager/zypper.go rename to pkg/manager/zypper.go diff --git a/internal/repos/find.go b/pkg/repos/find.go similarity index 94% rename from internal/repos/find.go rename to pkg/repos/find.go index eadafcb..b0be320 100644 --- a/internal/repos/find.go +++ b/pkg/repos/find.go @@ -18,10 +18,10 @@ package repos -import "go.elara.ws/lure/internal/db" +import "go.elara.ws/lure/pkg/db" // FindPkgs looks for packages matching the inputs inside the database. -// It returns a map that maps the package name input to the packages found for it. +// It returns a map that maps the package name input to any packages found for it. // It also returns a slice that contains the names of all packages that were not found. func FindPkgs(pkgs []string) (map[string][]db.Package, []string, error) { found := map[string][]db.Package{} diff --git a/internal/repos/find_test.go b/pkg/repos/find_test.go similarity index 98% rename from internal/repos/find_test.go rename to pkg/repos/find_test.go index af1d9cc..6ab8ac4 100644 --- a/internal/repos/find_test.go +++ b/pkg/repos/find_test.go @@ -24,9 +24,9 @@ import ( "strings" "testing" - "go.elara.ws/lure/internal/db" - "go.elara.ws/lure/internal/repos" "go.elara.ws/lure/internal/types" + "go.elara.ws/lure/pkg/db" + "go.elara.ws/lure/pkg/repos" ) func TestFindPkgs(t *testing.T) { diff --git a/internal/repos/pull.go b/pkg/repos/pull.go similarity index 95% rename from internal/repos/pull.go rename to pkg/repos/pull.go index a06914d..96175c1 100644 --- a/internal/repos/pull.go +++ b/pkg/repos/pull.go @@ -34,13 +34,13 @@ import ( "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/format/diff" "github.com/pelletier/go-toml/v2" - "go.elara.ws/lure/distro" - "go.elara.ws/lure/internal/config" - "go.elara.ws/lure/internal/db" "go.elara.ws/lure/internal/log" "go.elara.ws/lure/internal/shutils" "go.elara.ws/lure/internal/shutils/decoder" "go.elara.ws/lure/internal/types" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/db" + "go.elara.ws/lure/pkg/distro" "go.elara.ws/vercmp" "mvdan.cc/sh/v3/expand" "mvdan.cc/sh/v3/interp" @@ -49,7 +49,7 @@ import ( // Pull pulls the provided repositories. If a repo doesn't exist, it will be cloned // and its packages will be written to the DB. If it does exist, it will be pulled. -// In this case, only changed packages will be processed. +// In this case, only changed packages will be processed if possible. func Pull(ctx context.Context, repos []types.Repo) error { for _, repo := range repos { repoURL, err := url.Parse(repo.URL) @@ -149,9 +149,11 @@ func Pull(ctx context.Context, repos []types.Repo) error { } fl.Close() - currentVer, _, _ := strings.Cut(config.Version, "-") - if vercmp.Compare(currentVer, repoCfg.Repo.MinVersion) == -1 { - log.Warn("LURE repo's minumum LURE version is greater than the current version. Try updating LURE if something doesn't work.").Str("repo", repo.Name).Send() + if config.Version != "unknown" { + currentVer, _, _ := strings.Cut(config.Version, "-") + if vercmp.Compare(currentVer, repoCfg.Repo.MinVersion) == -1 { + log.Warn("LURE repo's minumum LURE version is greater than the current version. Try updating LURE if something doesn't work.").Str("repo", repo.Name).Send() + } } } diff --git a/internal/repos/pull_test.go b/pkg/repos/pull_test.go similarity index 95% rename from internal/repos/pull_test.go rename to pkg/repos/pull_test.go index 6c1c927..54cf915 100644 --- a/internal/repos/pull_test.go +++ b/pkg/repos/pull_test.go @@ -24,10 +24,10 @@ import ( "path/filepath" "testing" - "go.elara.ws/lure/internal/config" - "go.elara.ws/lure/internal/db" - "go.elara.ws/lure/internal/repos" "go.elara.ws/lure/internal/types" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/db" + "go.elara.ws/lure/pkg/repos" ) func setCfgDirs(t *testing.T) { diff --git a/repo.go b/repo.go index 97ce5e0..c37afd7 100644 --- a/repo.go +++ b/repo.go @@ -24,11 +24,11 @@ import ( "github.com/pelletier/go-toml/v2" "github.com/urfave/cli/v2" - "go.elara.ws/lure/internal/config" - "go.elara.ws/lure/internal/db" "go.elara.ws/lure/internal/log" - "go.elara.ws/lure/internal/repos" "go.elara.ws/lure/internal/types" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/db" + "go.elara.ws/lure/pkg/repos" "golang.org/x/exp/slices" ) diff --git a/scripts/gen-version.sh b/scripts/gen-version.sh deleted file mode 100755 index 9eb213e..0000000 --- a/scripts/gen-version.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -git describe --tags > version.txt \ No newline at end of file diff --git a/upgrade.go b/upgrade.go index 78ecccc..dd16009 100644 --- a/upgrade.go +++ b/upgrade.go @@ -23,14 +23,14 @@ import ( "fmt" "github.com/urfave/cli/v2" - "go.elara.ws/lure/distro" - "go.elara.ws/lure/internal/build" - "go.elara.ws/lure/internal/config" - "go.elara.ws/lure/internal/db" "go.elara.ws/lure/internal/log" - "go.elara.ws/lure/internal/repos" "go.elara.ws/lure/internal/types" - "go.elara.ws/lure/manager" + "go.elara.ws/lure/pkg/build" + "go.elara.ws/lure/pkg/config" + "go.elara.ws/lure/pkg/db" + "go.elara.ws/lure/pkg/distro" + "go.elara.ws/lure/pkg/manager" + "go.elara.ws/lure/pkg/repos" "go.elara.ws/vercmp" "golang.org/x/exp/maps" "golang.org/x/exp/slices"