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

feat: initial config framework #17

Merged
merged 1 commit into from
Feb 9, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: go-test
strategy:
matrix:
go-version: [1.20.x, 1.21.x]
go-version: [1.21.x, 1.22.x]
platform: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.20.x
go-version: 1.21.x
- name: Build binary
run: GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }} make build
- name: Upload release asset
Expand Down
23 changes: 22 additions & 1 deletion cmd/cardano-up/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Blink Labs Software
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@
package main

import (
"log/slog"
"os"

"github.com/spf13/cobra"
Expand All @@ -25,6 +26,10 @@ const (
)

func main() {
globalFlags := struct {
debug bool
}{}

rootCmd := &cobra.Command{
Use: programName,
/*
Expand All @@ -36,11 +41,27 @@ func main() {
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
*/
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Configure default logger
logLevel := slog.LevelInfo
if globalFlags.debug {
logLevel = slog.LevelDebug
}
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: logLevel,
}))
slog.SetDefault(logger)
},
}

// Global flags
rootCmd.PersistentFlags().BoolVarP(&globalFlags.debug, "debug", "D", false, "enable debug logging")

// Add subcommands
rootCmd.AddCommand(
versionCommand(),
// TODO: remove me
testCommand(),
)

if err := rootCmd.Execute(); err != nil {
Expand Down
39 changes: 39 additions & 0 deletions cmd/cardano-up/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2023 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"log/slog"
"os"

"github.com/blinklabs-io/cardano-up/pkgmgr"
"github.com/spf13/cobra"
)

func testCommand() *cobra.Command {
return &cobra.Command{
Use: "test",
Short: "Test subcommand for development work (TODO: remove me)",
Run: func(cmd *cobra.Command, args []string) {
pm, err := pkgmgr.NewDefaultPackageManager()
if err != nil {
slog.Error(fmt.Sprintf("failed to create package manager: %s", err))
os.Exit(1)
}
slog.Debug(fmt.Sprintf("pm = %#v", pm))
},
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/blinklabs-io/cardano-up

go 1.20
go 1.21

require github.com/spf13/cobra v1.8.0

Expand Down
57 changes: 57 additions & 0 deletions pkgmgr/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pkgmgr

import (
"fmt"
"log/slog"
"os"
"path/filepath"
)

type Config struct {
ConfigDir string
CacheDir string
Logger *slog.Logger
}

func NewDefaultConfig() (Config, error) {
userConfigDir, err := os.UserConfigDir()
if err != nil {
return Config{}, fmt.Errorf(
"could not determine user config directory: %s",
err,
)
}
userCacheDir, err := os.UserCacheDir()
if err != nil {
return Config{}, fmt.Errorf(
"could not determine user cache directory: %s",
err,
)
}
ret := Config{
ConfigDir: filepath.Join(
userConfigDir,
"cardano-up",
),
CacheDir: filepath.Join(
userCacheDir,
"cardano-up",
),
Logger: slog.Default(),
}
return ret, nil
}
136 changes: 136 additions & 0 deletions pkgmgr/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package pkgmgr_test

import (
"os"
"path/filepath"
"runtime"
"testing"

"github.com/blinklabs-io/cardano-up/pkgmgr"
)

func TestNewDefaultConfig(t *testing.T) {
testHome := "/path/to/user/home"
var expectedCacheDir, expectedConfigDir string
switch runtime.GOOS {
case "linux":
expectedCacheDir = filepath.Join(testHome, ".cache/cardano-up")
expectedConfigDir = filepath.Join(testHome, ".config/cardano-up")
case "darwin":
expectedCacheDir = filepath.Join(testHome, "Library/Caches/cardano-up")
expectedConfigDir = filepath.Join(testHome, "Library/Application Support/cardano-up")
default:
t.Fatalf("unsupported OS: %s", runtime.GOOS)
}
origEnvVars := setEnvVars(
map[string]string{
"HOME": testHome,
"XDG_CONFIG_HOME": "",
"XDG_CACHE_HOME": "",
},
)
defer func() {
setEnvVars(origEnvVars)
}()
cfg, err := pkgmgr.NewDefaultConfig()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if cfg.CacheDir != expectedCacheDir {
t.Fatalf(
"did not get expected cache dir, got %q, expected %q",
cfg.CacheDir,
expectedCacheDir,
)
}
if cfg.ConfigDir != expectedConfigDir {
t.Fatalf(
"did not get expected config dir, got %q, expected %q",
cfg.ConfigDir,
expectedConfigDir,
)
}
}

func TestNewDefaultConfigXdgConfigCacheEnvVars(t *testing.T) {
testHome := "/path/to/user/home"
testXdgCacheHome := filepath.Join(testHome, ".cache-test")
testXdgConfigHome := filepath.Join(testHome, ".config-test")
var expectedCacheDir, expectedConfigDir string
switch runtime.GOOS {
case "linux":
expectedCacheDir = filepath.Join(testXdgCacheHome, "cardano-up")
expectedConfigDir = filepath.Join(testXdgConfigHome, "cardano-up")
case "darwin":
expectedCacheDir = filepath.Join(testHome, "Library/Caches/cardano-up")
expectedConfigDir = filepath.Join(testHome, "Library/Application Support/cardano-up")
default:
t.Fatalf("unsupported OS: %s", runtime.GOOS)
}
origEnvVars := setEnvVars(
map[string]string{
"HOME": testHome,
"XDG_CONFIG_HOME": testXdgConfigHome,
"XDG_CACHE_HOME": testXdgCacheHome,
},
)
defer func() {
setEnvVars(origEnvVars)
}()
cfg, err := pkgmgr.NewDefaultConfig()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if cfg.CacheDir != expectedCacheDir {
t.Fatalf(
"did not get expected cache dir, got %q, expected %q",
cfg.CacheDir,
expectedCacheDir,
)
}
if cfg.ConfigDir != expectedConfigDir {
t.Fatalf(
"did not get expected config dir, got %q, expected %q",
cfg.ConfigDir,
expectedConfigDir,
)
}
}

func TestNewDefaultConfigEmptyHome(t *testing.T) {
expectedErrs := map[string]string{
"linux": "could not determine user config directory: neither $XDG_CONFIG_HOME nor $HOME are defined",
"darwin": "could not determine user config directory: $HOME is not defined",
}
origEnvVars := setEnvVars(
map[string]string{
"HOME": "",
"XDG_CONFIG_HOME": "",
"XDG_CACHE_HOME": "",
},
)
defer func() {
setEnvVars(origEnvVars)
}()
_, err := pkgmgr.NewDefaultConfig()
expectedErr, ok := expectedErrs[runtime.GOOS]
if !ok {
t.Fatalf("unsupported OS: %s", runtime.GOOS)
}
if err == nil || err.Error() != expectedErr {
t.Fatalf(
"did not get expected error, got %q, expected %q",
err.Error(),
expectedErr,
)
}
}

func setEnvVars(envVars map[string]string) map[string]string {
origVars := map[string]string{}
for k, v := range envVars {
origVars[k] = os.Getenv(k)
os.Setenv(k, v)
}
return origVars
}
44 changes: 44 additions & 0 deletions pkgmgr/pkgmgr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pkgmgr

type PackageManager struct {
config Config
// TODO
}

func NewPackageManager(cfg Config) (*PackageManager, error) {
p := &PackageManager{
config: cfg,
}
if err := p.init(); err != nil {
return nil, err
}
return p, nil
}

func NewDefaultPackageManager() (*PackageManager, error) {
pmCfg, err := NewDefaultConfig()
if err != nil {
return nil, err
}
return NewPackageManager(pmCfg)
}

func (p *PackageManager) init() error {
// TODO: create config/cache dirs
p.config.Logger.Debug("initializing package manager")
return nil
}