Skip to content

Commit d159b92

Browse files
committed
setup wizard implemented
1 parent a4aa187 commit d159b92

File tree

3 files changed

+84
-23
lines changed

3 files changed

+84
-23
lines changed

main.go

+24-17
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package main
22

33
import (
44
"log"
5+
"os"
56
"strings"
67
"time"
78

89
"go-nostrss/nostr"
10+
"go-nostrss/types"
911
"go-nostrss/utils"
1012

1113
"github.com/mmcdole/gofeed"
@@ -22,23 +24,33 @@ func FetchRSSFeed(url string) ([]*gofeed.Item, error) {
2224
}
2325

2426
func main() {
25-
// Load configuration
26-
config, err := utils.LoadConfig("config.yml")
27-
if err != nil {
28-
log.Fatalf("Error loading configuration: %v", err)
27+
const configFileName = "config.yml"
28+
29+
var config *types.Config
30+
if _, err := os.Stat(configFileName); os.IsNotExist(err) {
31+
log.Println("Configuration file not found. Starting setup wizard...")
32+
var setupErr error
33+
config, setupErr = utils.SetupConfig(configFileName)
34+
if setupErr != nil {
35+
log.Fatalf("Error setting up configuration: %v", setupErr)
36+
}
37+
} else {
38+
var loadErr error
39+
config, loadErr = utils.LoadConfig(configFileName)
40+
if loadErr != nil {
41+
log.Fatalf("Error loading configuration: %v", loadErr)
42+
}
2943
}
3044

31-
// Load cache
3245
cache, err := utils.LoadCache(config.CacheFile)
3346
if err != nil {
3447
log.Fatalf("Error loading cache: %v", err)
3548
}
3649

37-
// Main loop
3850
ticker := time.NewTicker(time.Duration(config.FetchIntervalMins) * time.Minute)
3951
defer ticker.Stop()
4052

41-
for {
53+
for range ticker.C {
4254
items, err := FetchRSSFeed(config.RSSFeed)
4355
if err != nil {
4456
log.Printf("Error fetching RSS feed: %v", err)
@@ -47,25 +59,21 @@ func main() {
4759

4860
for _, item := range items {
4961
cache.Mu.Lock()
50-
alreadyPosted := cache.PostedLinks[item.Link]
51-
cache.Mu.Unlock()
52-
53-
if alreadyPosted {
62+
if cache.PostedLinks[item.Link] {
63+
cache.Mu.Unlock()
5464
continue
5565
}
66+
cache.Mu.Unlock()
5667

57-
// Prepare event content
5868
content := strings.TrimSpace(item.Title) + "\n" + item.Link
5969

60-
// Use the article's publish time for the event's created_at field
6170
var createdAt int64
6271
if item.PublishedParsed != nil {
6372
createdAt = item.PublishedParsed.Unix()
6473
} else {
65-
createdAt = time.Now().Unix() // Fallback to current time if not available
74+
createdAt = time.Now().Unix()
6675
}
6776

68-
// Create Nostr event with the article's publish time and content
6977
event, err := nostr.CreateNostrEvent(content, config.NostrPublicKey, createdAt)
7078
if err != nil {
7179
log.Printf("Error creating Nostr event: %v", err)
@@ -89,7 +97,6 @@ func main() {
8997
if err != nil {
9098
log.Printf("Error saving cache: %v", err)
9199
}
92-
93-
<-ticker.C
94100
}
101+
95102
}

utils/cache.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ func SaveCache(filename string, cache *types.Cache) error {
4040
log.Printf("Saving cache to file: %s", filename)
4141

4242
cache.Mu.Lock()
43-
log.Println("Cache lock acquired for saving")
43+
//log.Println("Cache lock acquired for saving")
4444
defer func() {
4545
cache.Mu.Unlock()
46-
log.Println("Cache lock released after saving")
46+
//log.Println("Cache lock released after saving")
4747
}()
4848

4949
data, err := json.Marshal(cache)
@@ -52,9 +52,6 @@ func SaveCache(filename string, cache *types.Cache) error {
5252
return fmt.Errorf("failed to serialize cache: %w", err)
5353
}
5454

55-
_ = os.Rename(filename, filename+".bak") // Optional backup
56-
log.Println("Backup created for cache file")
57-
5855
err = os.WriteFile(filename, data, 0644)
5956
if err != nil {
6057
log.Printf("Error writing cache to file: %v", err)
@@ -63,4 +60,4 @@ func SaveCache(filename string, cache *types.Cache) error {
6360

6461
log.Println("Cache saved successfully")
6562
return nil
66-
}
63+
}

utils/wizard.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package utils
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"log"
7+
"os"
8+
"strconv"
9+
"strings"
10+
11+
"go-nostrss/types"
12+
13+
"gopkg.in/yaml.v3"
14+
)
15+
16+
// PromptForInput prompts the user for input and returns the response
17+
func PromptForInput(prompt string) string {
18+
fmt.Print(prompt)
19+
reader := bufio.NewReader(os.Stdin)
20+
response, err := reader.ReadString('\n')
21+
if err != nil {
22+
log.Fatalf("Error reading input: %v", err)
23+
}
24+
return strings.TrimSpace(response)
25+
}
26+
27+
// PromptForInt prompts the user for an integer input with validation
28+
func PromptForInt(prompt string) int {
29+
for {
30+
input := PromptForInput(prompt)
31+
value, err := strconv.Atoi(input)
32+
if err != nil || value <= 0 {
33+
fmt.Println("Invalid input. Please enter a positive integer.")
34+
continue
35+
}
36+
return value
37+
}
38+
}
39+
40+
// SetupConfig initializes the configuration through user input
41+
func SetupConfig(filename string) (*types.Config, error) {
42+
var config types.Config
43+
44+
config.RSSFeed = PromptForInput("Enter the URL of the RSS Feed: ")
45+
config.NostrPrivateKey = PromptForInput("Enter your Private Key in Hex Format: ")
46+
config.NostrPublicKey = PromptForInput("Enter your Public Key in Hex Format: ")
47+
config.RelayURL = PromptForInput("Enter the Relay URL (e.g., wss://relay.example.com): ")
48+
config.FetchIntervalMins = PromptForInt("Enter the Fetch Interval in minutes: ")
49+
config.CacheFile = "posted_articles.json" // Default value
50+
51+
data, err := yaml.Marshal(&config)
52+
if err != nil {
53+
return nil, err
54+
}
55+
err = os.WriteFile(filename, data, 0644)
56+
return &config, err
57+
}

0 commit comments

Comments
 (0)