|
| 1 | +// |
| 2 | +// This file is part of arduino-connector |
| 3 | +// |
| 4 | +// Copyright (C) 2017 Arduino AG (http://www.arduino.cc/) |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | + |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "encoding/json" |
| 23 | + "fmt" |
| 24 | + |
| 25 | + apt "github.com/arduino/go-apt-client" |
| 26 | + mqtt "github.com/eclipse/paho.mqtt.golang" |
| 27 | +) |
| 28 | + |
| 29 | +// AptRepositoryListEvent sends a list of available repositories |
| 30 | +func (s *Status) AptRepositoryListEvent(client mqtt.Client, msg mqtt.Message) { |
| 31 | + // Get packages from system |
| 32 | + all, err := apt.ParseAPTConfigFolder("/etc/apt") |
| 33 | + if err != nil { |
| 34 | + s.Error("/apt/repos/list/error", fmt.Errorf("Retrieving repositories: %s", err)) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + // Send result |
| 39 | + data, err := json.Marshal(all) |
| 40 | + if err != nil { |
| 41 | + s.Error("/apt/repos/list/error", fmt.Errorf("Json marshal result: %s", err)) |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + //var out bytes.Buffer |
| 46 | + //json.Indent(&out, data, "", " ") |
| 47 | + //fmt.Println(string(out.Bytes())) |
| 48 | + |
| 49 | + s.Info("/apt/repos/list", string(data)+"\n") |
| 50 | +} |
| 51 | + |
| 52 | +// AptRepositoryAddEvent adds a repository to the apt configuration |
| 53 | +func (s *Status) AptRepositoryAddEvent(client mqtt.Client, msg mqtt.Message) { |
| 54 | + var params struct { |
| 55 | + Repository *apt.Repository `json:"repository"` |
| 56 | + } |
| 57 | + err := json.Unmarshal(msg.Payload(), ¶ms) |
| 58 | + if err != nil { |
| 59 | + s.Error("/apt/repos/add/error", fmt.Errorf("Unmarshal '%s': %s", msg.Payload(), err)) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + err = apt.AddRepository(params.Repository, "/etc/apt") |
| 64 | + if err != nil { |
| 65 | + s.Error("/apt/repos/add/error", fmt.Errorf("Adding repository '%s': %s", msg.Payload(), err)) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + s.InfoCommandOutput("/apt/repos/add", []byte("OK"), nil) |
| 70 | +} |
| 71 | + |
| 72 | +// AptRepositoryRemoveEvent removes a repository from the apt configuration |
| 73 | +func (s *Status) AptRepositoryRemoveEvent(client mqtt.Client, msg mqtt.Message) { |
| 74 | + var params struct { |
| 75 | + Repository *apt.Repository `json:"repository"` |
| 76 | + } |
| 77 | + err := json.Unmarshal(msg.Payload(), ¶ms) |
| 78 | + if err != nil { |
| 79 | + s.Error("/apt/repos/remove/error", fmt.Errorf("Unmarshal '%s': %s", msg.Payload(), err)) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + err = apt.RemoveRepository(params.Repository, "/etc/apt") |
| 84 | + if err != nil { |
| 85 | + s.Error("/apt/repos/remove/error", fmt.Errorf("Removing repository '%s': %s", msg.Payload(), err)) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + s.InfoCommandOutput("/apt/repos/remove", []byte("OK"), nil) |
| 90 | +} |
| 91 | + |
| 92 | +// AptRepositoryEditEvent modifies a repository definition in the apt configuration |
| 93 | +func (s *Status) AptRepositoryEditEvent(client mqtt.Client, msg mqtt.Message) { |
| 94 | + var params struct { |
| 95 | + OldRepository *apt.Repository `json:"old_repository"` |
| 96 | + NewRepository *apt.Repository `json:"new_repository"` |
| 97 | + } |
| 98 | + err := json.Unmarshal(msg.Payload(), ¶ms) |
| 99 | + if err != nil { |
| 100 | + s.Error("/apt/repos/edit/error", fmt.Errorf("Unmarshal '%s': %s", msg.Payload(), err)) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + err = apt.EditRepository(params.OldRepository, params.NewRepository, "/etc/apt") |
| 105 | + if err != nil { |
| 106 | + s.Error("/apt/repos/edit/error", fmt.Errorf("Changing repository '%s': %s", msg.Payload(), err)) |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + s.InfoCommandOutput("/apt/repos/edit", []byte("OK"), nil) |
| 111 | +} |
0 commit comments