Cleaning up errors and settings
Consistency on returns and program flow
This commit is contained in:
parent
fd171d0928
commit
234b0e848e
1 changed files with 26 additions and 18 deletions
|
@ -45,12 +45,11 @@ const (
|
||||||
desiredIP6Method = "ignore"
|
desiredIP6Method = "ignore"
|
||||||
)
|
)
|
||||||
|
|
||||||
func printVersion() {
|
func printVersion() error {
|
||||||
/* Create new instance of gonetworkmanager */
|
/* Create new instance of gonetworkmanager */
|
||||||
nm, err := gonetworkmanager.NewNetworkManager()
|
nm, err := gonetworkmanager.NewNetworkManager()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
return err
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't really need the network manager object per se
|
// Don't really need the network manager object per se
|
||||||
|
@ -58,24 +57,24 @@ func printVersion() {
|
||||||
var nmVersion string
|
var nmVersion string
|
||||||
nmVersion, err = nm.GetPropertyVersion()
|
nmVersion, err = nm.GetPropertyVersion()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
return err
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Network Manager Version: " + nmVersion)
|
fmt.Println("Network Manager Version: " + nmVersion)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkForExistingConnection() error {
|
func checkForExistingConnection() (bool, error) {
|
||||||
// See if our connection already exists
|
// See if our connection already exists
|
||||||
everactiveSettings, err := gonetworkmanager.NewSettings()
|
settings, err := gonetworkmanager.NewSettings()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
currentConnections, err := everactiveSettings.ListConnections()
|
currentConnections, err := settings.ListConnections()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range currentConnections {
|
for _, v := range currentConnections {
|
||||||
|
@ -86,10 +85,10 @@ func checkForExistingConnection() error {
|
||||||
}
|
}
|
||||||
currentConnectionSection := connectionSettings[connectionSection]
|
currentConnectionSection := connectionSettings[connectionSection]
|
||||||
if currentConnectionSection[connectionSectionID] == connectionID {
|
if currentConnectionSection[connectionSectionID] == connectionID {
|
||||||
return fmt.Errorf("connection already exists")
|
return true, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createNewConnection() error {
|
func createNewConnection() error {
|
||||||
|
@ -102,8 +101,7 @@ func createNewConnection() error {
|
||||||
connection[connectionSection][connectionSectionType] = ethernetType
|
connection[connectionSection][connectionSectionType] = ethernetType
|
||||||
connectionUUID, err := uuid.NewUUID()
|
connectionUUID, err := uuid.NewUUID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
return err
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
connection[connectionSection][connectionSectionUUID] = connectionUUID.String()
|
connection[connectionSection][connectionSectionUUID] = connectionUUID.String()
|
||||||
connection[connectionSection][connectionSectionIfaceName] = interfaceName
|
connection[connectionSection][connectionSectionIfaceName] = interfaceName
|
||||||
|
@ -135,13 +133,13 @@ func createNewConnection() error {
|
||||||
connection[ip6Section] = make(map[string]interface{})
|
connection[ip6Section] = make(map[string]interface{})
|
||||||
connection[ip6Section][ip6SectionMethod] = desiredIP6Method
|
connection[ip6Section][ip6SectionMethod] = desiredIP6Method
|
||||||
|
|
||||||
everactiveSettings, err := gonetworkmanager.NewSettings()
|
settings, err := gonetworkmanager.NewSettings()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = everactiveSettings.AddConnection(connection)
|
_, err = settings.AddConnection(connection)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -152,16 +150,26 @@ func createNewConnection() error {
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// show the version
|
// show the version
|
||||||
printVersion()
|
if printVersion() != nil {
|
||||||
|
fmt.Println("failed to find version. Is NetworkManager running?")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
// See if our connection already exists
|
// See if our connection already exists
|
||||||
err := checkForExistingConnection()
|
doesExist, err := checkForExistingConnection()
|
||||||
|
|
||||||
|
// if an error then we are done.
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if the connection already exists we are done.
|
||||||
|
if doesExist == true {
|
||||||
|
fmt.Println("connection already exists, nothing to do.")
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
// create the new connection
|
// create the new connection
|
||||||
err = createNewConnection()
|
err = createNewConnection()
|
||||||
|
|
||||||
|
|
Reference in a new issue