forked from talkkonnect/talkkonnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoprovision.go
109 lines (94 loc) · 2.76 KB
/
autoprovision.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
/*
* talkkonnect headless mumble client/gateway with lcd screen and channel control
* Copyright (C) 2018-2019, Suvir Kumar <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* talkkonnect is the based on talkiepi and barnard by Daniel Chote and Tim Cooper
*
* The Initial Developer of the Original Code is
* Suvir Kumar <[email protected]>
* Portions created by the Initial Developer are Copyright (C) Suvir Kumar. All Rights Reserved.
*
* Contributor(s):
*
* Suvir Kumar <[email protected]>
*
* My Blog is at www.talkkonnect.com
* The source code is hosted at github.com/talkkonnect
*
* autoprovision.go -- Autoprovisioning function for talkkonnect
*
*/
package talkkonnect
import (
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
)
func autoProvision() error {
if len(TkID) < 8 {
var err error
var macaddress []string
macaddress, err = getMacAddr()
if err != nil {
return errors.New("TkID Configuration Provisioning XML Filenaame Not Found And Cannot Get Mac Address ")
}
for _, a := range macaddress {
re, err := regexp.Compile(`(:)`)
if err != nil {
FatalCleanUp(err.Error())
}
TkID = re.ReplaceAllString(a, "")
}
}
if string(TkID[len(TkID)-4]) != ".xml" {
TkID = TkID + ".xml"
}
if string(URL[len(URL)-1]) != "/" {
URL = URL + "/"
}
if string(SaveFilePath[len(SaveFilePath)-1]) != "/" {
SaveFilePath = SaveFilePath + "/"
}
fileURL := URL + TkID
log.Println("info: Trying to Autoprovision with URL: ", fileURL)
err := downloadFile(SaveFilePath, SaveFilename, fileURL)
if err != nil {
return fmt.Errorf("error: DownloadFile Module Returned an Error: %q", err.Error())
}
return nil
}
func downloadFile(SaveFilePath string, SaveFilename string, URL string) error {
resp, err := http.Get(URL)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
log.Println("debug: HTTP Provisioning Server Responded With Status 200 OK ")
} else {
return fmt.Errorf("error: HTTP Provisioning Server Returned Status %q %q", resp.StatusCode, http.StatusText(resp.StatusCode))
}
out, err := os.Create(SaveFilePath + SaveFilename)
if err != nil {
return fmt.Errorf("error: Cannot Create File Error: %q", err.Error())
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return fmt.Errorf("error: Cannot Copy File Error: %q", err.Error())
}
return nil
}