aboutsummaryrefslogtreecommitdiffstats
path: root/qt-cli/util/util.go
blob: a17db2711865fa755cb9e3eb8c27c3afea745df7 (plain)
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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only

package util

import (
	"fmt"
	"io"
	"io/fs"
	"os"
	"path/filepath"
)

type StringAnyMap map[string]interface{}

func (m *StringAnyMap) Merge(other StringAnyMap) {
	for k, v := range other {
		(*m)[k] = v
	}
}

func ReadAllFromFS(targetFS fs.FS, path string) ([]byte, error) {
	stat, err := fs.Stat(targetFS, path)
	if err != nil {
		return []byte{},
			fmt.Errorf("cannot read file info, given %v", path)
	}

	if !stat.Mode().IsRegular() {
		return []byte{},
			fmt.Errorf("cannot read non-regular file, given = %v", path)
	}

	file, err := targetFS.Open(path)
	if err != nil {
		return []byte{}, err
	}

	defer file.Close()
	return io.ReadAll(file)
}

func WriteAll(data []byte, destPath string) (int, error) {
	dir := filepath.Dir(destPath)
	if err := os.MkdirAll(dir, os.ModePerm); err != nil {
		return 0, err
	}

	destFile, err := os.Create(destPath)
	if err != nil {
		return 0, err
	}

	defer destFile.Close()
	return destFile.Write(data)
}

func PrintlnWithName(data string, fileName string) {
	fmt.Println(">>>>>>>", fileName)
	fmt.Print(data)
	fmt.Println("<<<<<<<", fileName)
}

func Msg(s string) string {
	return s
}