This repository has been archived by the owner on Nov 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
95 lines (84 loc) · 2.98 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"errors"
"log"
"os"
"time"
"github.com/smartystreets/raptr/cli"
"github.com/smartystreets/raptr/config"
"github.com/smartystreets/raptr/manifest"
"github.com/smartystreets/raptr/messages"
"github.com/smartystreets/raptr/storage"
"github.com/smartystreets/raptr/tasks"
)
func main() {
log.SetFlags(log.Lshortfile | log.Lmicroseconds)
if command, configFile := cli.ReadMessage(); command == nil {
log.Println("[ERROR] Unable to determine CLI command")
os.Exit(1)
} else if configuration, err := config.LoadConfiguration(configFile); err != nil {
log.Println("[ERROR] Unable to load configuration:", err)
os.Exit(1)
// } else if err := configuration.Validate(command); err != nil {
// log.Println("[ERROR] Invalid configuration:", err)
// os.Exit(1)
} else if err := executeCommand(configuration, command); err != nil {
log.Println("[ERROR] Command Failed:", err)
os.Exit(1)
}
}
func executeCommand(configuration config.Configuration, message interface{}) error {
switch message.(type) {
case messages.UploadCommand:
return executeUpload(configuration, message.(messages.UploadCommand))
case messages.LinkCommand:
return executeLink(configuration, message.(messages.LinkCommand))
case messages.UnlinkCommand:
return errors.New("Not implemented")
case messages.CleanCommand:
return errors.New("Not implemented")
default:
return errors.New("Not implemented")
}
}
func executeUpload(configuration config.Configuration, command messages.UploadCommand) error {
remote, found := configuration.Open(command.StorageName)
if !found {
return errors.New("Remote storage specified was not found in the configuration file.")
}
finder := manifest.NewLocalPackageFinder()
task := tasks.NewUploadTask(remote.Storage)
files, err := finder.Find(command.PackagePath)
if err != nil {
return err
} else if len(files) == 0 {
log.Println("[INFO] No files found; nothing to do.")
return nil
} else {
// TODO: package name could be optional in one of the following scenarios:
// 1. we have a DSC file
// 2. there's only one package
// otherwise, the name would be required.
return task.Upload(command.Category, command.PackageName, files)
}
}
func executeLink(configuration config.Configuration, command messages.LinkCommand) error {
remote, found := configuration.Open(command.StorageName)
if !found {
return errors.New("Remote storage specified was not found in the configuration file.")
}
layout := remote.Layout
task := tasks.NewLinkTask(remote.Storage, layout.Categories, layout.Architectures)
for i := 0; i < maxConcurrencyAttempts; i++ {
err := task.Link(command.Distribution, command.Category, command.PackageName, command.PackageVersion)
if err == nil {
return nil
} else if err != storage.ConcurrencyError {
return err
}
log.Printf("[WARN] Concurrency error, sleeping before trying again [%d/%d]\n", i+1, maxConcurrencyAttempts)
time.Sleep(time.Second * 1)
}
return storage.ConcurrencyError
}
const maxConcurrencyAttempts = 5