-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
116 lines (107 loc) · 3.04 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
// Written by Mikhail P. Ortiz-Lunyov (mportizlunyov)
//
// Version 1.0.0-release (August 19th 2024)
//
// This script is licensed under the GNU Public License v3 (GPLv3)
// Intended for use on Linux to check the specific distro running, using native Linux tools.
// This is useful when developing programs to adapt to specific linux environments.
//
// This is the front-end that uses the functions from the Linux Distro Engine.
// This is only compiled/run if being used as a stand-alone program.
/*
Linux Distro Engine (Go edition) main package.
This package serves as a stand-alone application of the linuxdistroengine go package.
It exists to demonstrate the capabilities of the linuxdistroengine package.
*/
package main
// Import packages
import (
"flag"
"fmt"
"os"
linuxdistorengine "github.com/mportizlunyov/linuxdistroengine-go/linuxdistroengine"
)
// Script-level fields
// Main method of the program.
//
// This method declares the flags and sets the default value to use the
// linuxdistroengine package.
func main() {
// Declare variables and their defaults
var option string = "id" // Default argument for the engine
var argsCount int = 0
// Check for flags
// // -v / --version
versionShort := flag.Bool("v", false, "Print the version number of this module")
versionLong := flag.Bool("version", false, "Long form of [-v]")
// // -pn / --pretty-name
prettynameShort := flag.Bool("pn", false, "Print the full 'Pretty Name' of the distro, if applicable")
prettynameLong := flag.Bool("pretty-name", false, "Long form of [-pn]")
// // -k / --kernel
kernelShort := flag.Bool("k", false, "Print the kernel version")
kernelLong := flag.Bool("kernel", false, "Long form of [-v]")
// // Parse flags
flag.Parse()
// // Finalise flags
prettynameFlag := *prettynameLong || *prettynameShort
kernelFlag := *kernelLong || *kernelShort
versionFlag := *versionShort || *versionLong
// Prepare options based on parameter
switch true {
case versionFlag:
option = "v"
argsCount++
}
switch true {
case prettynameFlag:
option = "pn"
argsCount++
}
switch true {
case kernelFlag:
option = "k"
argsCount++
}
// Define final action based on parameters
switch argsCount {
case 0:
fallthrough
case 1:
pkgResult, pkgErrCode := linuxdistorengine.DistroResult(option)
switch pkgErrCode {
case 0:
fmt.Println(pkgResult)
case 1:
fallthrough
case 2:
fallthrough
case 3:
fallthrough
case 4:
fallthrough
case 5:
switch pkgErrCode {
case 1:
fmt.Println("This program is intended to run on Linux")
case 2:
fmt.Println("[uname -r] command failed!")
case 3:
fmt.Println("Reading [os-release] file failed")
case 4:
fmt.Println("Neither ID nor PRETTY_NAME found in */os-release file")
case 5:
fmt.Println("Bad argument for DistroResult() method [ ", option, " ]")
}
os.Exit(1)
case 44:
fmt.Println(pkgResult)
os.Exit(44)
default:
fmt.Println("INTERNAL ERROR, MISSING ERROR CODE [", pkgErrCode, "]")
os.Exit(254)
}
default:
fmt.Println("Incompatible arguments")
os.Exit(1)
}
}