yats.git

ref: b1f040c9b39c19c44685571366f9d7db7f6e3c5d

client/io.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
/**
 * Yats - yats
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Paolo Lulli <kevwe.com>
 * @copyright Paolo Lulli 2024
 */
package main

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"os"
)

func mapFromBase64(value string) (map[string]any, bool) {
	decodedBytes, err := base64.StdEncoding.DecodeString(value)
	if err != nil {
		fmt.Println("Error decoding string:", err)
		return nil, true
	}

	decoded := string(decodedBytes)
	var dataMap map[string]any
	json.Unmarshal([]byte(decoded), &dataMap)
	return dataMap, false
}

func getMaxpage(list string) int64 {
	var result map[string]int64
	json.Unmarshal([]byte(list), &result)
	return result["maxpage"]
}

func getDataJson(list string) ([]byte, bool) {
	var result map[string]interface{}
	json.Unmarshal([]byte(list), &result)
	innerData := result["data"]

	dataJson, er := json.Marshal(innerData)
	if er != nil {
		fmt.Printf("Parsing response: %v", er)
		return nil, true
	}
	return dataJson, false
}

func readSecretPin(prompt string) string {
	var input string
	fmt.Printf("%s\n", prompt)
	_, err := fmt.Scan(&input)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(1)
	}
	return input
}

func fileExists(fileName string) bool {
	_, error := os.Stat(fileName)

	if os.IsNotExist(error) {
		return false
	}
	return true
}