-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection30.go
133 lines (123 loc) · 4.09 KB
/
connection30.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
/*
* Copyright (C) 2019 Paul Seyfert
* Author: Paul Seyfert <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"flag"
"fmt"
"os"
"github.com/godbus/dbus"
nm2qr "github.com/pseyfert/go-networkmanager-qrcode-generator/qrcode_for_nm_connection"
ux "github.com/pseyfert/go-networkmanager-qrcode-generator/ux"
)
func validformat(s string) bool {
return s == "png" || s == "plain" || s == "string"
}
func main() {
dbusConnection, err := dbus.SystemBus()
if err != nil {
fmt.Printf("ERROR: couldn't connect to system dbus\n")
os.Exit(9)
}
var outputname string
var connectionId int
var connectionName string
var format string
var exactMatch bool
var listConnections bool
flag.StringVar(&outputname, "o", "network.png", "output filename")
flag.StringVar(&format, "f", "png", "output format (allowed: png, string, plain)")
flag.IntVar(&connectionId, "i", -1, "network manager connection Id to visualize")
flag.StringVar(&connectionName, "n", "", "network manager connection name to visualize")
flag.BoolVar(&exactMatch, "e", false, "matches by name must be exact (fuzzy by default)")
flag.BoolVar(&listConnections, "l", false, "list connection names and quit")
flag.Parse()
if !validformat(format) {
fmt.Printf("ERROR: invalid format requested: %s\n", format)
os.Exit(8)
}
if listConnections {
fmt.Printf("the following connections are known:\n")
cons, err := ux.AllConnections(dbusConnection)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
os.Exit(8)
}
for _, con := range cons {
fmt.Printf("%s:\tSSID %s\n", con.Id, con.Ssid)
}
os.Exit(0)
}
if connectionId < 0 && connectionName == "" {
fmt.Printf("ERROR: specify either a connection ID or a connection name")
os.Exit(7)
}
var networkSettings nm2qr.NetworkSetting
if connectionId >= 0 {
ids, err := ux.ConnectionIDs(dbusConnection)
if nil != err {
fmt.Printf("could not obtain list of connections: %v\n", err)
fmt.Print("continuing\n")
} else {
found := false
for _, id := range ids {
if id == connectionId {
found = true
break
}
}
if !found {
fmt.Printf("%d is not in the list of known connections. trying anyway.\n", connectionId)
}
networkSettings, err = nm2qr.GetNetworkSettings(connectionId, dbusConnection)
}
} else {
networkSettings, err = ux.BestMatch(connectionName, dbusConnection)
if exactMatch && !(networkSettings.Id == connectionName || string(networkSettings.Ssid) == connectionName) {
fmt.Printf("%s is not in the list of known connections.\n", connectionName)
os.Exit(3)
}
}
if nil != err {
fmt.Printf("something went wrong in network setting retrival, %v\n", err)
os.Exit(1)
}
if format == "plain" {
qr := nm2qr.NetworkCode(networkSettings)
fmt.Printf("QR code should contain:\n%s\n", qr)
} else if format == "string" {
qr, err := nm2qr.QRNetworkCode(networkSettings)
if nil != err {
fmt.Printf("something went wrong in qr code generation, %v\n", err)
os.Exit(2)
}
// fmt.Printf("QR code as string:\n%s\n", nm2qr.CompressQR(qr.ToString(false)))
fmt.Printf("QR code as string:\n%s\n", qr.ToString(false))
fmt.Printf("QR code as string:\n%s\n", qr.ToSmallString(false))
} else {
qr, err := nm2qr.QRNetworkCode(networkSettings)
if nil != err {
fmt.Printf("something went wrong in qr code generation, %v\n", err)
os.Exit(2)
}
err = qr.WriteFile(-5, outputname)
if nil != err {
fmt.Printf("something went wrong in qr code storing, %v\n", err)
os.Exit(3)
}
}
}