Skip to content
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
27 changes: 22 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/container-registry/harbor-satellite/internal/watcher"
"github.com/container-registry/harbor-satellite/pkg/config"
"os"
"path/filepath"

_ "github.com/joho/godotenv/autoload"
"github.com/rs/zerolog"
Expand All @@ -39,12 +40,14 @@ func main() {
var token string
var useUnsecure bool
var mirrors mirrorFlags
var configDir string

flag.StringVar(&groundControlURL, "ground-control-url", "", "URL to ground control")
flag.BoolVar(&jsonLogging, "json-logging", true, "Enable JSON logging")
flag.StringVar(&token, "token", "", "Satellite token")
flag.BoolVar(&useUnsecure, "use-unsecure", false, "Use insecure (HTTP) connections to registries")
flag.Var(&mirrors, "mirrors", "Specify CRI and registries in the form CRI:registry1,registry2")
flag.StringVar(&configDir, "config-dir", "", "Directory for satellite config and state files")

flag.Parse()

Expand All @@ -57,13 +60,27 @@ func main() {
if !useUnsecure {
useUnsecure = os.Getenv("USE_UNSECURE") == "true"
}
if configDir == "" {
configDir = os.Getenv("CONFIG_DIR")
}
if configDir == "" {
configDir = config.DefaultConfigDir
}

if err := os.MkdirAll(configDir, 0o755); err != nil {
fmt.Printf("Error creating config directory: %v", err)
os.Exit(1)
}
Comment on lines +70 to +73
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Error message missing newline.

The error message on line 71 lacks a trailing newline, which is inconsistent with similar error messages in this file (e.g., lines 85, 99, 105).

Proposed fix
 	if err := os.MkdirAll(configDir, 0o755); err != nil {
-		fmt.Printf("Error creating config directory: %v", err)
+		fmt.Printf("Error creating config directory: %v\n", err)
 		os.Exit(1)
 	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if err := os.MkdirAll(configDir, 0o755); err != nil {
fmt.Printf("Error creating config directory: %v", err)
os.Exit(1)
}
if err := os.MkdirAll(configDir, 0o755); err != nil {
fmt.Printf("Error creating config directory: %v\n", err)
os.Exit(1)
}
🤖 Prompt for AI Agents
In `@cmd/main.go` around lines 70 - 73, The error print for the os.MkdirAll call
(the fmt.Printf("Error creating config directory: %v", err) next to the
configDir creation) is missing a trailing newline; update that call to include a
newline (e.g., fmt.Printf("Error creating config directory: %v\n", err) or use
fmt.Fprintf(os.Stderr, "...\\n", err)) so it matches the style of the other
error messages in this file.


configPath := filepath.Join(configDir, config.DefaultConfigFilename)
prevConfigPath := filepath.Join(configDir, config.DefaultPrevConfigFilename)

if token == "" || groundControlURL == "" {
fmt.Println("Missing required arguments: --token and --ground-control-url or matching env vars.")
os.Exit(1)
}

cm, _, err := config.InitConfigManager(token, groundControlURL, config.DefaultConfigPath, config.DefaultPrevConfigPath, jsonLogging, useUnsecure)
cm, _, err := config.InitConfigManager(token, groundControlURL, configPath, prevConfigPath, jsonLogging, useUnsecure)
if err != nil {
fmt.Printf("Error initiating the config manager: %v", err)
os.Exit(1)
Expand All @@ -83,19 +100,19 @@ func main() {
os.Exit(1)
}

err = run(jsonLogging, token, groundControlURL, useUnsecure)
err = run(jsonLogging, token, groundControlURL, useUnsecure, configPath, prevConfigPath)
if err != nil {
fmt.Printf("fatal: %v\n", err)
os.Exit(1)
}
}

func run(jsonLogging bool, token, groundControlURL string, useUnsecure bool) error {
func run(jsonLogging bool, token, groundControlURL string, useUnsecure bool, configPath, prevConfigPath string) error {
ctx, cancel := utils.SetupContext(context.Background())
defer cancel()
wg, ctx := errgroup.WithContext(ctx)

cm, warnings, err := config.InitConfigManager(token, groundControlURL, config.DefaultConfigPath, config.DefaultPrevConfigPath, jsonLogging, useUnsecure)
cm, warnings, err := config.InitConfigManager(token, groundControlURL, configPath, prevConfigPath, jsonLogging, useUnsecure)
if err != nil {
fmt.Printf("Error initiating the config manager: %v", err)
return err
Expand Down Expand Up @@ -124,7 +141,7 @@ func run(jsonLogging bool, token, groundControlURL string, useUnsecure bool) err

// Watch for changes in the config file
wg.Go(func() error {
return watcher.WatchChanges(ctx, log.With().Str("component", "file watcher").Logger(), config.DefaultConfigPath, eventChan)
return watcher.WatchChanges(ctx, log.With().Str("component", "file watcher").Logger(), configPath, eventChan)
})

// Watch for changes in the config file
Expand Down
19 changes: 17 additions & 2 deletions pkg/config/constants.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package config

import (
"os"
"path/filepath"
)

// Job names that the user is expected to provide in the config.json file
const ReplicateStateJobName string = "replicate_state"
const ZTRConfigJobName string = "register_satellite"
Expand All @@ -9,8 +14,18 @@ const ZTRConfigJobName string = "register_satellite"
// in the config.json file.

// Default config.json path for the satellite, used if the user does not provide any config path
const DefaultConfigPath string = "config.json"
const DefaultPrevConfigPath string = "prev_config.json"
const DefaultConfigFilename string = "config.json"
const DefaultPrevConfigFilename string = "prev_config.json"

var DefaultConfigDir string = defaultConfigDir()

func defaultConfigDir() string {
home, err := os.UserHomeDir()
if err != nil || home == "" {
return filepath.Join(".", ".config", "satellite")
}
return filepath.Join(home, ".config", "satellite")
}

// Below are the default values of the job schedules that would be used if the user does not provide any schedule or
// if there is any error while parsing the cron expression
Expand Down