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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cli
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/mendersoftware/mender-artifact/areader"
"github.com/mendersoftware/mender-artifact/artifact"
"github.com/mendersoftware/mender-artifact/handlers"
"github.com/mendersoftware/mender-artifact/utils"
)
var defaultIndentation = " "
func sortedKeys(mapWithKeys interface{}) sort.StringSlice {
var keys sort.StringSlice
mapVal := reflect.ValueOf(mapWithKeys)
if mapVal.Kind() != reflect.Map {
return nil
}
keys = make([]string, mapVal.Len())
keysVal := mapVal.MapKeys()
for i, keyVal := range keysVal {
keys[i] = keyVal.String()
}
keys.Sort()
return keys
}
func printList(title string, iterable []string, err string, shouldFlow bool, indentationLevel int) {
fmt.Printf("%s%s:", strings.Repeat(defaultIndentation, indentationLevel), title)
if len(err) > 0 {
fmt.Printf("%s\n", err)
} else if len(iterable) == 0 {
fmt.Printf(" []\n")
} else if shouldFlow {
fmt.Printf(" [%s]\n", strings.Join(iterable, ", "))
} else {
fmt.Printf("\n")
for _, value := range iterable {
fmt.Printf("%s- %s\n", strings.Repeat(defaultIndentation, indentationLevel+1), value)
}
}
}
func printObject(
title string,
someObject map[string]interface{},
err string,
indentationLevel int,
) {
fmt.Printf("%s%s:", strings.Repeat(defaultIndentation, indentationLevel), title)
if len(err) > 0 {
fmt.Printf("%s\n", err)
} else if len(someObject) == 0 {
fmt.Printf(" {}\n")
} else {
fmt.Printf("\n")
keys := sortedKeys(someObject)
for _, key := range keys {
fmt.Printf("%s%s: %s\n",
strings.Repeat(defaultIndentation, indentationLevel+1), key, (someObject)[key])
}
}
}
func printUnnamedObject(
someObject map[string]interface{},
indentationLevel int,
) {
if len(someObject) == 0 {
fmt.Printf("%s- {}\n", strings.Repeat(defaultIndentation, indentationLevel))
} else {
keys := sortedKeys(someObject)
for index, key := range keys {
entry := fmt.Sprintf("%s: %s", key, (someObject)[key])
if index == 0 {
fmt.Printf("%s- %s\n", strings.Repeat(defaultIndentation, indentationLevel), entry)
continue
}
// here we assume indentationLevel is 2 spaces to increase by the 2 character length
// the list indicator "- " inserted above has
fmt.Printf("%s%s\n", strings.Repeat(defaultIndentation, indentationLevel+1), entry)
}
}
}
func printHeader(ar *areader.Reader, sigInfo string, indentationLevel int) {
info := ar.GetInfo()
fmt.Printf("%sMender Artifact:\n", strings.Repeat(defaultIndentation, indentationLevel))
fmt.Printf(
"%sName: %s\n",
strings.Repeat(defaultIndentation, indentationLevel+1),
ar.GetArtifactName(),
)
fmt.Printf(
"%sFormat: %s\n",
strings.Repeat(defaultIndentation, indentationLevel+1),
info.Format,
)
fmt.Printf(
"%sVersion: %d\n",
strings.Repeat(defaultIndentation, indentationLevel+1),
info.Version,
)
fmt.Printf("%sSignature: %s\n", strings.Repeat(defaultIndentation, indentationLevel+1), sigInfo)
printList("Compatible devices", ar.GetCompatibleDevices(), "", true, indentationLevel+1)
}
func printStateScripts(scripts []string, indentationLevel int) {
printList("State scripts", scripts, "", false, indentationLevel)
}
func printFiles(files []*handlers.DataFile, indentationLevel int) {
if len(files) == 0 {
fmt.Printf("%sFiles: []\n", strings.Repeat(defaultIndentation, indentationLevel))
} else {
fmt.Printf("%sFiles:\n", strings.Repeat(defaultIndentation, indentationLevel))
for _, f := range files {
data := map[string]interface{}{
"name": f.Name,
"size": fmt.Sprintf("%d", f.Size),
"modified": f.Date,
"checksum": f.Checksum,
}
printUnnamedObject(data, indentationLevel+1)
}
}
}
func printProvides(p handlers.Installer, indentationLevel int) {
provides, err := p.GetUpdateProvides()
error := ""
if err != nil {
error = fmt.Sprintf(" Invalid provides section: %s", err.Error())
}
providesWorkaround := make(map[string]interface{}, len(provides))
for k, v := range provides {
providesWorkaround[k] = v
}
printObject("Provides", providesWorkaround, error, indentationLevel)
}
func printDepends(p handlers.Installer, indentationLevel int) {
depends, err := p.GetUpdateDepends()
error := ""
if err != nil {
error = fmt.Sprintf(" Invalid depends section: %s", err.Error())
}
printObject("Depends", depends, error, indentationLevel)
}
func printClearsProvides(p handlers.Installer, indentationLevel int) {
caps := p.GetUpdateClearsProvides()
printList("Clears Provides", caps, "", true, indentationLevel)
}
func printUpdateMetadata(p handlers.Installer, indentationLevel int) {
metaData, err := p.GetUpdateMetaData()
fmt.Printf("%sMetadata:", strings.Repeat(defaultIndentation, indentationLevel))
if err != nil {
fmt.Printf(" Invalid metadata section: %s\n", err.Error())
} else if len(metaData) == 0 {
fmt.Printf(" {}\n")
} else {
var metaDataSlice []byte
if err == nil {
metaDataSlice, err = json.Marshal(metaData)
}
var metaDataBuf bytes.Buffer
if err == nil {
err = json.Indent(
&metaDataBuf,
metaDataSlice,
strings.Repeat(defaultIndentation, indentationLevel+1),
defaultIndentation)
}
if err != nil {
fmt.Printf(" Invalid metadata section: %s\n", err.Error())
} else {
fmt.Printf("\n")
fmt.Printf(
"%s%s\n",
strings.Repeat(defaultIndentation, indentationLevel+1),
metaDataBuf.String())
}
}
}
func printType(p handlers.Installer, indentationLevel int) {
updateType := p.GetUpdateType()
if updateType == nil {
emptyType := "Empty type"
updateType = &emptyType
}
fmt.Printf(
"%s- Type: %v\n",
strings.Repeat(defaultIndentation, indentationLevel),
*updateType,
)
}
func printPayload(p handlers.Installer, indentationLevel int) {
// here we assume indentationLevel is 2 spaces so the initial entry can omit
// the indentation increase and rely on the 2 character length of the list item indicator "- "
printType(p, indentationLevel)
printProvides(p, indentationLevel+1)
printDepends(p, indentationLevel+1)
printClearsProvides(p, indentationLevel+1)
printUpdateMetadata(p, indentationLevel+1)
printFiles(p.GetUpdateAllFiles(), indentationLevel+1)
}
func printUpdates(updatePayloads map[int]handlers.Installer, indentationLevel int) {
fmt.Printf("%sUpdates:\n", strings.Repeat(defaultIndentation, indentationLevel))
for _, payload := range updatePayloads {
printPayload(payload, indentationLevel+1)
}
}
func readArtifact(c *cli.Context) error {
if c.NArg() == 0 {
return cli.NewExitError("Nothing specified, nothing read. \nMaybe you wanted"+
" to say 'artifacts read <pathspec>'?", errArtifactInvalidParameters)
}
f, err := os.Open(c.Args().First())
if err != nil {
return cli.NewExitError("Can not open artifact: "+c.Args().First(),
errArtifactOpen)
}
defer f.Close()
var verifyCallback areader.SignatureVerifyFn
key, err := getKey(c)
if err != nil {
return cli.NewExitError(err.Error(), errArtifactInvalidParameters)
}
if key != nil {
verifyCallback = key.Verify
}
// if key is not provided just continue reading artifact returning
// info that signature can not be verified
sigInfo := "no signature"
ver := func(message, sig []byte) error {
sigInfo = "signed but no key for verification provided; " +
"please use `-k` option for providing verification key"
if key != nil {
err = verifyCallback(message, sig)
if err != nil {
sigInfo = "signed; verification using provided key failed"
} else {
sigInfo = "signed and verified correctly"
}
}
return nil
}
var scripts []string
readScripts := func(r io.Reader, info os.FileInfo) error {
scripts = append(scripts, info.Name())
return nil
}
ar := areader.NewReader(f)
if !c.Bool("no-progress") {
fmt.Fprintln(os.Stderr, "Reading Artifact...")
ar.ProgressReader = utils.NewProgressReader()
}
ar.ScriptsReadCallback = readScripts
ar.VerifySignatureCallback = ver
err = ar.ReadArtifact()
if err != nil {
if errors.Cause(err) == artifact.ErrCompatibleDevices {
return cli.NewExitError("Invalid Artifact. No 'device-type' found.", 1)
}
return cli.NewExitError(err.Error(), 1)
}
printHeader(ar, sigInfo, 0)
provides := ar.GetArtifactProvides()
if provides != nil {
fmt.Printf("%sProvides group: %s\n", defaultIndentation, provides.ArtifactGroup)
}
depends := ar.GetArtifactDepends()
if depends != nil {
fmt.Printf(
"%sDepends on one of artifact(s): [%s]\n",
defaultIndentation, strings.Join(depends.ArtifactName, ", "),
)
fmt.Printf(
"%sDepends on one of group(s): [%s]\n",
defaultIndentation, strings.Join(depends.ArtifactGroup, ", "),
)
}
printStateScripts(scripts, 1)
fmt.Println()
updatePayloads := ar.GetHandlers()
printUpdates(updatePayloads, 0)
return nil
}
|