-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
178 lines (161 loc) · 4.84 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (C) 2017 by Max Ungless
// Main package for primegenerator. Generates primes.
package main
import (
"bufio"
"fmt"
// "io/ioutil"
"math/big"
"os"
"github.com/MaxTheMonster/PrimeNumberGenerator/client"
"github.com/MaxTheMonster/PrimeNumberGenerator/computation"
"github.com/MaxTheMonster/PrimeNumberGenerator/config"
"github.com/MaxTheMonster/PrimeNumberGenerator/primes"
"github.com/MaxTheMonster/PrimeNumberGenerator/server"
"github.com/MaxTheMonster/PrimeNumberGenerator/storage"
"github.com/urfave/cli"
)
const (
version = "1.3.7"
appName = "PrimeNumberGenerator"
appUsage = "Generate prime numbers forever"
descConfigure = "Runs auto-configuration wizard"
descCount = "Displays the estimated curren n prime numbers"
descRun = "Begins computation of primes"
descClient = "Launches a new instance of a client"
descServer = "Launches a new instance of a server"
appHelpTemplate = `{{if .VisibleCommands}}COMMANDS:{{range .VisibleCategories}}{{if .Name}}
{{.Name}}:{{end}}{{range .VisibleCommands}}
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
GLOBAL OPTIONS:
{{range $index, $option := .VisibleFlags}}{{if $index}}
{{end}}{{$option}}{{end}}{{end}}
`
)
// SetConfiguration sets the global configuration variables
func SetConfiguration() {
config.LocalConfig = config.GetUserConfig()
config.StartingPrime = config.LocalConfig.StartingPrime
config.MaxFilesize = config.LocalConfig.MaxFilesize
config.MaxBufferSize = config.LocalConfig.MaxBufferSize
config.ShowFails = config.LocalConfig.ShowFails
config.Host = config.LocalConfig.ServerIP
config.Address = config.Host + ":" + config.Port
}
// SetId sets the gloabl id variable
func SetId() {
config.Id = primes.GetCurrentId()
}
// SetLastPrimeGenerated sets the global lastprimegenerated variable
func SetLastPrimeGenerated() {
config.LastPrimeGenerated = getLastPrime()
}
// showHelp shows help to the user.
func showHelp() {
fmt.Println("COMMANDS")
fmt.Println("count \t Displays the total number of generated primes.")
fmt.Println("configure \t Generates a configuration for the program.")
fmt.Println("run \t Runs the program indefinitely.")
fmt.Println("version \t Displays the version of the program.")
fmt.Println("help \t Displays this screen. Gives help.")
os.Exit(1)
}
// showProgramDetails prints details about the program to STDOUT
func showProgramDetails() {
fmt.Printf("PrimeNumberGenerator %s LITE", version)
fmt.Println("\nCopyright (C) 2017-2018 by Max Ungless")
fmt.Println("This program comes with ABSOLUTELY NO WARRANTY.\nThis is free software, and you are welcome to redistribute it\nunder the condiditions set in the GNU General Public License version 3.\nSee the file named LICENSE for details.")
fmt.Println("\nFor bugs, send mail to max@maxungless.com")
fmt.Println()
}
// getLastPrime() searches for last generated prime
// in all prime storage files.
func getLastPrime() *big.Int {
latestFile := storage.OpenLatestFile(os.O_RDONLY, 0666)
defer latestFile.Close()
var lastPrimeGenerated string
scanner := bufio.NewScanner(latestFile)
for scanner.Scan() {
lastPrimeGenerated = scanner.Text()
}
if lastPrimeGenerated == "0" || lastPrimeGenerated == "" {
lastPrimeGenerated = config.StartingPrime
}
foundPrime := new(big.Int)
foundPrime.SetString(lastPrimeGenerated, 10)
return foundPrime
}
func init() {
// config.Logger.SetOutput(ioutil.Discard)
showProgramDetails()
SetConfiguration()
}
func main() {
app := cli.NewApp()
app.Name = appName
app.Usage = appUsage
app.Version = version
cli.AppHelpTemplate = appHelpTemplate
app.Commands = []cli.Command{
{
Name: "configure",
Aliases: []string{"cn"},
Usage: descConfigure,
Action: func(c *cli.Context) error {
config.RunConfigurator()
return nil
},
},
{
Name: "count",
Aliases: []string{"ct"},
Usage: descCount,
Before: func(c *cli.Context) error {
SetId()
return nil
},
Action: func(c *cli.Context) error {
primes.ShowCurrentCount()
return nil
},
},
{
Name: "run",
Aliases: []string{"r"},
Usage: descRun,
Before: func(c *cli.Context) error {
SetId()
SetLastPrimeGenerated()
return nil
},
Action: func(c *cli.Context) error {
computation.ComputePrimes(config.LastPrimeGenerated, true, true, big.NewInt(0))
return nil
},
},
{
Name: "client",
Aliases: []string{"cl"},
Usage: descClient,
Action: client.LaunchClient,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "heavy",
Usage: "Distribute individual divisions instead of distributing entire primes",
},
},
},
{
Name: "server",
Aliases: []string{"s"},
Usage: descServer,
Before: func(c *cli.Context) error {
SetId()
SetLastPrimeGenerated()
return nil
},
Action: server.LaunchServer,
},
}
app.Run(os.Args)
}