Skip to content

Commit 768966a

Browse files
Merge pull request #9 from canaria-computer/dev1
version 1.2.0 unstable merge
2 parents 33fb864 + 10ebe0c commit 768966a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3705
-1208
lines changed

README.md

Lines changed: 138 additions & 349 deletions
Large diffs are not rendered by default.

cmd/cache.go

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
11
/*
2-
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
3-
2+
Copyright © 2025 canaria-computer
43
*/
54
package cmd
65

76
import (
8-
"fmt"
9-
107
"github.com/spf13/cobra"
118
)
129

1310
// cacheCmd represents the cache command
1411
var cacheCmd = &cobra.Command{
1512
Use: "cache",
16-
Short: "A brief description of your command",
17-
Long: `A longer description that spans multiple lines and likely contains examples
18-
and usage of using your command. For example:
19-
20-
Cobra is a CLI library for Go that empowers applications.
21-
This application is a tool to generate the needed files
22-
to quickly create a Cobra application.`,
23-
Run: func(cmd *cobra.Command, args []string) {
24-
fmt.Println("cache called")
25-
},
26-
}
13+
Short: "Manage cached access information",
14+
Long: `Manage the cached access information used during evidence collection.
2715
28-
func init() {
29-
rootCmd.AddCommand(cacheCmd)
16+
The cache stores your IP address, geographic location, ISP, and ASN information
17+
to avoid repeated API calls to IP information providers. Cache is valid for 6 hours.
3018
31-
// Here you will define your flags and configuration settings.
19+
Available subcommands:
20+
show - Display current cached information
21+
clear - Remove cached information
22+
refresh - Force refresh the cache with new data
3223
33-
// Cobra supports Persistent Flags which will work for this command
34-
// and all subcommands, e.g.:
35-
// cacheCmd.PersistentFlags().String("foo", "", "A help for foo")
24+
Examples:
25+
# Show current cache
26+
down-force cache show
3627
37-
// Cobra supports local flags which will only run when this command
38-
// is called directly, e.g.:
39-
// cacheCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
28+
# Clear cache
29+
down-force cache clear
30+
31+
# Refresh cache
32+
down-force cache refresh
33+
`,
34+
}
35+
36+
func init() {
37+
rootCmd.AddCommand(cacheCmd)
4038
}

cmd/clear.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
11
/*
2-
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
3-
2+
Copyright © 2025 canaria-computer
43
*/
54
package cmd
65

76
import (
8-
"fmt"
7+
"os"
98

9+
"github.com/canaria-computer/down-force/internal/utils"
10+
"github.com/charmbracelet/log"
1011
"github.com/spf13/cobra"
1112
)
1213

1314
// clearCmd represents the clear command
1415
var clearCmd = &cobra.Command{
1516
Use: "clear",
16-
Short: "A brief description of your command",
17-
Long: `A longer description that spans multiple lines and likely contains examples
18-
and usage of using your command. For example:
19-
20-
Cobra is a CLI library for Go that empowers applications.
21-
This application is a tool to generate the needed files
22-
to quickly create a Cobra application.`,
23-
Run: func(cmd *cobra.Command, args []string) {
24-
fmt.Println("clear called")
25-
},
17+
Short: "Clear cached access information",
18+
Long: `Remove the cached access information file.
19+
20+
This forces the next evidence collection to fetch fresh access information
21+
from IP providers instead of using cached data.
22+
23+
Example:
24+
down-force cache clear
25+
`,
26+
Run: runCacheClear,
2627
}
2728

2829
func init() {
2930
cacheCmd.AddCommand(clearCmd)
31+
}
3032

31-
// Here you will define your flags and configuration settings.
32-
33-
// Cobra supports Persistent Flags which will work for this command
34-
// and all subcommands, e.g.:
35-
// clearCmd.PersistentFlags().String("foo", "", "A help for foo")
36-
37-
// Cobra supports local flags which will only run when this command
38-
// is called directly, e.g.:
39-
// clearCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
33+
func runCacheClear(cmd *cobra.Command, args []string) {
34+
cachePath := utils.GetCachePath()
35+
36+
if err := os.Remove(cachePath); err != nil {
37+
if os.IsNotExist(err) {
38+
log.Info("No cache to clear")
39+
return
40+
}
41+
log.Fatalf("Failed to clear cache: %v", err)
42+
}
43+
44+
log.Info("✅ Cache cleared successfully")
4045
}

cmd/complete.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,10 @@ func init() {
4949
func runComplete(cmd *cobra.Command, args []string) {
5050
configPath := args[0]
5151

52-
// Check if config file exists
5352
if _, err := os.Stat(configPath); os.IsNotExist(err) {
5453
log.Fatalf("❌ Config file not found: %s", configPath)
5554
}
5655

57-
// Load and complete config
5856
cfg, modified, err := config.LoadAndCompleteConfig(configPath)
5957
if err != nil {
6058
log.Fatalf("❌ Failed to load config: %v", err)
@@ -65,16 +63,16 @@ func runComplete(cmd *cobra.Command, args []string) {
6563
return
6664
}
6765

68-
// Show what was completed
6966
fmt.Println("📝 Changes to be applied:")
7067
fmt.Println()
7168

7269
for _, ua := range cfg.UserAgents {
7370
builtin := config.GetBuiltinUserAgent(ua.Name)
74-
if builtin != nil && ua.UserAgentString == builtin.UserAgentString {
71+
if builtin != nil && ua.UserAgentString == builtin.Name {
7572
fmt.Printf(" • %s\n", ua.Name)
7673
fmt.Printf(" UA: %s...\n", truncateString(ua.UserAgentString, 60))
77-
fmt.Printf(" Viewport: %dx%d\n", ua.ViewportWidth, ua.ViewportHeight)
74+
fmt.Printf(" Viewport (Horiz): %dx%d\n", ua.Screen.Horizontal.Width, ua.Screen.Horizontal.Height)
75+
fmt.Printf(" Viewport (Vert): %dx%d\n", ua.Screen.Vertical.Width, ua.Screen.Vertical.Height)
7876
fmt.Println()
7977
}
8078
}
@@ -84,7 +82,6 @@ func runComplete(cmd *cobra.Command, args []string) {
8482
return
8583
}
8684

87-
// Save the completed config
8885
if err := config.SaveConfig(configPath, cfg); err != nil {
8986
log.Fatalf("❌ Failed to save config: %v", err)
9087
}

cmd/config.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
var configCmd = &cobra.Command{
8+
Use: "config",
9+
Short: "Manage application configuration",
10+
Long: `Manage application behavior configuration. Validate, initialize, and view current configuration values.`,
11+
}
12+
13+
func init() {
14+
rootCmd.AddCommand(configCmd)
15+
}

cmd/config_init.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/canaria-computer/down-force/internal/appconfig"
9+
"github.com/charmbracelet/log"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var (
14+
forceOverwrite bool
15+
)
16+
17+
var configInitCmd = &cobra.Command{
18+
Use: "init",
19+
Short: "Generate default configuration file",
20+
Long: `Generate default configuration file at ~/.config/down-force/config.yaml. Requires --force-overwrite flag to overwrite existing file.`,
21+
RunE: runConfigInit,
22+
}
23+
24+
func init() {
25+
configCmd.AddCommand(configInitCmd)
26+
configInitCmd.Flags().BoolVar(&forceOverwrite, "force-overwrite", false, "Force overwrite existing configuration file")
27+
}
28+
29+
func runConfigInit(cmd *cobra.Command, args []string) error {
30+
configPath, err := appconfig.GetDefaultConfigPath()
31+
if err != nil {
32+
return fmt.Errorf("failed to get config path: %w", err)
33+
}
34+
35+
if _, err := os.Stat(configPath); err == nil {
36+
if !forceOverwrite {
37+
return fmt.Errorf("config file already exists: %s\nUse --force-overwrite flag to overwrite existing file", configPath)
38+
}
39+
log.Warn("Overwriting existing configuration file", "path", configPath)
40+
}
41+
42+
configDir := filepath.Dir(configPath)
43+
if err := os.MkdirAll(configDir, 0700); err != nil {
44+
return fmt.Errorf("failed to create config directory: %w", err)
45+
}
46+
47+
if err := os.WriteFile(configPath, []byte(appconfig.ConfigTemplate), 0600); err != nil {
48+
return fmt.Errorf("failed to write config file: %w", err)
49+
}
50+
51+
log.Info("✓ Configuration file created", "path", configPath)
52+
return nil
53+
}

0 commit comments

Comments
 (0)