Skip to content

Commit

Permalink
Merge pull request #141 from tisnik/use-specific-type-for-exit-code
Browse files Browse the repository at this point in the history
Use specific type for exit code
  • Loading branch information
tisnik authored Jul 27, 2020
2 parents ba2cd86 + 91b06eb commit 66c3d51
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
23 changes: 16 additions & 7 deletions docs/packages/smart_proxy.html
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@
<div class="ident">proxy_content</div> <div class="literal">&#34;github.com/RedHatInsights/insights-results-smart-proxy/content&#34;</div><div class="operator"></div>
<div class="operator">)</div><div class="operator"></div>

</code></pre></td>
</tr>

<tr class="section">
<td class="doc"><p>ExitCode represents numeric value returned to parent process when the
current process finishes</p>
</td>
<td class="code"><pre><code><div class="keyword">type</div> <div class="ident">ExitCode</div> <div class="ident">int</div><div class="operator"></div>

<div class="keyword">const</div> <div class="operator">(</div>
</code></pre></td>
</tr>
Expand Down Expand Up @@ -212,7 +221,7 @@
<tr class="section">
<td class="doc"><p>printHelp function displays help on the standard output.</p>
</td>
<td class="code"><pre><code><div class="keyword">func</div> <div class="ident">printHelp</div><div class="operator">(</div><div class="operator">)</div> <div class="ident">int</div> <div class="operator">{</div>
<td class="code"><pre><code><div class="keyword">func</div> <div class="ident">printHelp</div><div class="operator">(</div><div class="operator">)</div> <div class="ident">ExitCode</div> <div class="operator">{</div>
<div class="ident">fmt</div><div class="operator">.</div><div class="ident">Printf</div><div class="operator">(</div><div class="ident">helpMessageTemplate</div><div class="operator">,</div> <div class="ident">os</div><div class="operator">.</div><div class="ident">Args</div><div class="operator">[</div><div class="literal">0</div><div class="operator">]</div><div class="operator">)</div><div class="operator"></div>
<div class="keyword">return</div> <div class="ident">ExitStatusOK</div><div class="operator"></div>
<div class="operator">}</div><div class="operator"></div>
Expand All @@ -223,7 +232,7 @@
<tr class="section">
<td class="doc"><p>printConfig function displays loaded configuration on the standard output.</p>
</td>
<td class="code"><pre><code><div class="keyword">func</div> <div class="ident">printConfig</div><div class="operator">(</div><div class="operator">)</div> <div class="ident">int</div> <div class="operator">{</div>
<td class="code"><pre><code><div class="keyword">func</div> <div class="ident">printConfig</div><div class="operator">(</div><div class="operator">)</div> <div class="ident">ExitCode</div> <div class="operator">{</div>
<div class="ident">configBytes</div><div class="operator">,</div> <div class="ident">err</div> <div class="operator">:=</div> <div class="ident">json</div><div class="operator">.</div><div class="ident">MarshalIndent</div><div class="operator">(</div><div class="ident">conf</div><div class="operator">.</div><div class="ident">Config</div><div class="operator">,</div> <div class="literal">&#34;&#34;</div><div class="operator">,</div> <div class="literal">&#34; &#34;</div><div class="operator">)</div><div class="operator"></div>

<div class="keyword">if</div> <div class="ident">err</div> <div class="operator">!=</div> <div class="ident">nil</div> <div class="operator">{</div>
Expand All @@ -248,7 +257,7 @@
<tr class="section">
<td class="doc"><p>printEnv function prints all environment variables to standard output.</p>
</td>
<td class="code"><pre><code><div class="keyword">func</div> <div class="ident">printEnv</div><div class="operator">(</div><div class="operator">)</div> <div class="ident">int</div> <div class="operator">{</div>
<td class="code"><pre><code><div class="keyword">func</div> <div class="ident">printEnv</div><div class="operator">(</div><div class="operator">)</div> <div class="ident">ExitCode</div> <div class="operator">{</div>
<div class="keyword">for</div> <div class="ident">_</div><div class="operator">,</div> <div class="ident">keyVal</div> <div class="operator">:=</div> <div class="keyword">range</div> <div class="ident">os</div><div class="operator">.</div><div class="ident">Environ</div><div class="operator">(</div><div class="operator">)</div> <div class="operator">{</div>
<div class="ident">fmt</div><div class="operator">.</div><div class="ident">Println</div><div class="operator">(</div><div class="ident">keyVal</div><div class="operator">)</div><div class="operator"></div>
<div class="operator">}</div><div class="operator"></div>
Expand All @@ -262,7 +271,7 @@
<tr class="section">
<td class="doc"><p>startService function starts service and returns error code.</p>
</td>
<td class="code"><pre><code><div class="keyword">func</div> <div class="ident">startServer</div><div class="operator">(</div><div class="operator">)</div> <div class="ident">int</div> <div class="operator">{</div>
<td class="code"><pre><code><div class="keyword">func</div> <div class="ident">startServer</div><div class="operator">(</div><div class="operator">)</div> <div class="ident">ExitCode</div> <div class="operator">{</div>
<div class="ident">_</div> <div class="operator">=</div> <div class="ident">conf</div><div class="operator">.</div><div class="ident">GetSetupConfiguration</div><div class="operator">(</div><div class="operator">)</div><div class="operator"></div>
<div class="ident">serverCfg</div> <div class="operator">:=</div> <div class="ident">conf</div><div class="operator">.</div><div class="ident">GetServerConfiguration</div><div class="operator">(</div><div class="operator">)</div><div class="operator"></div>
<div class="ident">metricsCfg</div> <div class="operator">:=</div> <div class="ident">conf</div><div class="operator">.</div><div class="ident">GetMetricsConfiguration</div><div class="operator">(</div><div class="operator">)</div><div class="operator"></div>
Expand Down Expand Up @@ -327,7 +336,7 @@
<tr class="section">
<td class="doc"><p>handleCommand select the function to be called depending on command argument</p>
</td>
<td class="code"><pre><code><div class="keyword">func</div> <div class="ident">handleCommand</div><div class="operator">(</div><div class="ident">command</div> <div class="ident">string</div><div class="operator">)</div> <div class="ident">int</div> <div class="operator">{</div>
<td class="code"><pre><code><div class="keyword">func</div> <div class="ident">handleCommand</div><div class="operator">(</div><div class="ident">command</div> <div class="ident">string</div><div class="operator">)</div> <div class="ident">ExitCode</div> <div class="operator">{</div>
<div class="keyword">switch</div> <div class="ident">command</div> <div class="operator">{</div>
<div class="keyword">case</div> <div class="literal">&#34;start-service&#34;</div><div class="operator">:</div>
<div class="keyword">return</div> <div class="ident">startServer</div><div class="operator">(</div><div class="operator">)</div><div class="operator"></div>
Expand Down Expand Up @@ -374,7 +383,7 @@
<div class="ident">flag</div><div class="operator">.</div><div class="ident">Parse</div><div class="operator">(</div><div class="operator">)</div><div class="operator"></div>

<div class="keyword">if</div> <div class="ident">showHelp</div> <div class="operator">{</div>
<div class="ident">os</div><div class="operator">.</div><div class="ident">Exit</div><div class="operator">(</div><div class="ident">printHelp</div><div class="operator">(</div><div class="operator">)</div><div class="operator">)</div><div class="operator"></div>
<div class="ident">os</div><div class="operator">.</div><div class="ident">Exit</div><div class="operator">(</div><div class="ident">int</div><div class="operator">(</div><div class="ident">printHelp</div><div class="operator">(</div><div class="operator">)</div><div class="operator">)</div><div class="operator">)</div><div class="operator"></div>
<div class="operator">}</div><div class="operator"></div>

<div class="keyword">if</div> <div class="ident">showVersion</div> <div class="operator">{</div>
Expand All @@ -389,7 +398,7 @@
<div class="ident">command</div> <div class="operator">=</div> <div class="ident">strings</div><div class="operator">.</div><div class="ident">ToLower</div><div class="operator">(</div><div class="ident">strings</div><div class="operator">.</div><div class="ident">TrimSpace</div><div class="operator">(</div><div class="ident">args</div><div class="operator">[</div><div class="literal">0</div><div class="operator">]</div><div class="operator">)</div><div class="operator">)</div><div class="operator"></div>
<div class="operator">}</div><div class="operator"></div>

<div class="ident">os</div><div class="operator">.</div><div class="ident">Exit</div><div class="operator">(</div><div class="ident">handleCommand</div><div class="operator">(</div><div class="ident">command</div><div class="operator">)</div><div class="operator">)</div><div class="operator"></div>
<div class="ident">os</div><div class="operator">.</div><div class="ident">Exit</div><div class="operator">(</div><div class="ident">int</div><div class="operator">(</div><div class="ident">handleCommand</div><div class="operator">(</div><div class="ident">command</div><div class="operator">)</div><div class="operator">)</div><div class="operator">)</div><div class="operator"></div>
<div class="operator">}</div><div class="operator"></div>

</code></pre></td>
Expand Down
6 changes: 3 additions & 3 deletions docs/packages/smart_proxy_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
<td class="doc"><p>TestPrintHelp checks that printing help returns OK exit code.</p>
</td>
<td class="code"><pre><code><div class="keyword">func</div> <div class="ident">TestPrintHelp</div><div class="operator">(</div><div class="ident">t</div> <div class="operator">*</div><div class="ident">testing</div><div class="operator">.</div><div class="ident">T</div><div class="operator">)</div> <div class="operator">{</div>
<div class="ident">assert</div><div class="operator">.</div><div class="ident">Equal</div><div class="operator">(</div><div class="ident">t</div><div class="operator">,</div> <div class="ident">main</div><div class="operator">.</div><div class="ident">ExitStatusOK</div><div class="operator">,</div> <div class="ident">main</div><div class="operator">.</div><div class="ident">PrintHelp</div><div class="operator">(</div><div class="operator">)</div><div class="operator">)</div><div class="operator"></div>
<div class="ident">assert</div><div class="operator">.</div><div class="ident">Equal</div><div class="operator">(</div><div class="ident">t</div><div class="operator">,</div> <div class="ident">main</div><div class="operator">.</div><div class="ident">ExitStatusOK</div><div class="operator">,</div> <div class="ident">int</div><div class="operator">(</div><div class="ident">main</div><div class="operator">.</div><div class="ident">PrintHelp</div><div class="operator">(</div><div class="operator">)</div><div class="operator">)</div><div class="operator">)</div><div class="operator"></div>
<div class="operator">}</div><div class="operator"></div>

</code></pre></td>
Expand All @@ -216,7 +216,7 @@
<td class="doc"><p>TestPrintConfig checks that printing configuration info returns OK exit code.</p>
</td>
<td class="code"><pre><code><div class="keyword">func</div> <div class="ident">TestPrintConfig</div><div class="operator">(</div><div class="ident">t</div> <div class="operator">*</div><div class="ident">testing</div><div class="operator">.</div><div class="ident">T</div><div class="operator">)</div> <div class="operator">{</div>
<div class="ident">assert</div><div class="operator">.</div><div class="ident">Equal</div><div class="operator">(</div><div class="ident">t</div><div class="operator">,</div> <div class="ident">main</div><div class="operator">.</div><div class="ident">ExitStatusOK</div><div class="operator">,</div> <div class="ident">main</div><div class="operator">.</div><div class="ident">PrintConfig</div><div class="operator">(</div><div class="operator">)</div><div class="operator">)</div><div class="operator"></div>
<div class="ident">assert</div><div class="operator">.</div><div class="ident">Equal</div><div class="operator">(</div><div class="ident">t</div><div class="operator">,</div> <div class="ident">main</div><div class="operator">.</div><div class="ident">ExitStatusOK</div><div class="operator">,</div> <div class="ident">int</div><div class="operator">(</div><div class="ident">main</div><div class="operator">.</div><div class="ident">PrintConfig</div><div class="operator">(</div><div class="operator">)</div><div class="operator">)</div><div class="operator">)</div><div class="operator"></div>
<div class="operator">}</div><div class="operator"></div>

</code></pre></td>
Expand All @@ -226,7 +226,7 @@
<td class="doc"><p>TestPrintEnv checks that printing environment variables returns OK exit code.</p>
</td>
<td class="code"><pre><code><div class="keyword">func</div> <div class="ident">TestPrintEnv</div><div class="operator">(</div><div class="ident">t</div> <div class="operator">*</div><div class="ident">testing</div><div class="operator">.</div><div class="ident">T</div><div class="operator">)</div> <div class="operator">{</div>
<div class="ident">assert</div><div class="operator">.</div><div class="ident">Equal</div><div class="operator">(</div><div class="ident">t</div><div class="operator">,</div> <div class="ident">main</div><div class="operator">.</div><div class="ident">ExitStatusOK</div><div class="operator">,</div> <div class="ident">main</div><div class="operator">.</div><div class="ident">PrintEnv</div><div class="operator">(</div><div class="operator">)</div><div class="operator">)</div><div class="operator"></div>
<div class="ident">assert</div><div class="operator">.</div><div class="ident">Equal</div><div class="operator">(</div><div class="ident">t</div><div class="operator">,</div> <div class="ident">main</div><div class="operator">.</div><div class="ident">ExitStatusOK</div><div class="operator">,</div> <div class="ident">int</div><div class="operator">(</div><div class="ident">main</div><div class="operator">.</div><div class="ident">PrintEnv</div><div class="operator">(</div><div class="operator">)</div><div class="operator">)</div><div class="operator">)</div><div class="operator"></div>
<div class="operator">}</div><div class="operator"></div>

</code></pre></td>
Expand Down
18 changes: 11 additions & 7 deletions smart_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ import (
proxy_content "github.com/RedHatInsights/insights-results-smart-proxy/content"
)

// ExitCode represents numeric value returned to parent process when the
// current process finishes
type ExitCode int

const (
// ExitStatusOK means that the service have finished with success
ExitStatusOK = iota
Expand Down Expand Up @@ -68,13 +72,13 @@ The commands are:
var serverInstance *server.HTTPServer

// printHelp function displays help on the standard output.
func printHelp() int {
func printHelp() ExitCode {
fmt.Printf(helpMessageTemplate, os.Args[0])
return ExitStatusOK
}

// printConfig function displays loaded configuration on the standard output.
func printConfig() int {
func printConfig() ExitCode {
configBytes, err := json.MarshalIndent(conf.Config, "", " ")

if err != nil {
Expand All @@ -89,7 +93,7 @@ func printConfig() int {
}

// printEnv function prints all environment variables to standard output.
func printEnv() int {
func printEnv() ExitCode {
for _, keyVal := range os.Environ() {
fmt.Println(keyVal)
}
Expand All @@ -98,7 +102,7 @@ func printEnv() int {
}

// startService function starts service and returns error code.
func startServer() int {
func startServer() ExitCode {
_ = conf.GetSetupConfiguration()
serverCfg := conf.GetServerConfiguration()
metricsCfg := conf.GetMetricsConfiguration()
Expand Down Expand Up @@ -153,7 +157,7 @@ func updateGroupInfo(servicesConf services.Configuration, groupsChannel chan []g
}

// handleCommand select the function to be called depending on command argument
func handleCommand(command string) int {
func handleCommand(command string) ExitCode {
switch command {
case "start-service":
return startServer()
Expand Down Expand Up @@ -195,7 +199,7 @@ func main() {
flag.Parse()

if showHelp {
os.Exit(printHelp())
os.Exit(int(printHelp()))
}

if showVersion {
Expand All @@ -210,5 +214,5 @@ func main() {
command = strings.ToLower(strings.TrimSpace(args[0]))
}

os.Exit(handleCommand(command))
os.Exit(int(handleCommand(command)))
}
6 changes: 3 additions & 3 deletions smart_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ func TestPrintVersionInfo(t *testing.T) {

// TestPrintHelp checks that printing help returns OK exit code.
func TestPrintHelp(t *testing.T) {
assert.Equal(t, main.ExitStatusOK, main.PrintHelp())
assert.Equal(t, main.ExitStatusOK, int(main.PrintHelp()))
}

// TestPrintConfig checks that printing configuration info returns OK exit code.
func TestPrintConfig(t *testing.T) {
assert.Equal(t, main.ExitStatusOK, main.PrintConfig())
assert.Equal(t, main.ExitStatusOK, int(main.PrintConfig()))
}

// TestPrintEnv checks that printing environment variables returns OK exit code.
func TestPrintEnv(t *testing.T) {
assert.Equal(t, main.ExitStatusOK, main.PrintEnv())
assert.Equal(t, main.ExitStatusOK, int(main.PrintEnv()))
}

0 comments on commit 66c3d51

Please sign in to comment.