github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/internal/pkg/helper/view.go (about)

     1  package helper
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/360EntSecGroup-Skylar/excelize/v2"
     9  	"github.com/easysoft/zendata/internal/pkg/domain"
    10  	fileUtils "github.com/easysoft/zendata/pkg/utils/file"
    11  	i118Utils "github.com/easysoft/zendata/pkg/utils/i118"
    12  	logUtils "github.com/easysoft/zendata/pkg/utils/log"
    13  	"github.com/easysoft/zendata/pkg/utils/vari"
    14  	"github.com/mattn/go-runewidth"
    15  	"gopkg.in/yaml.v3"
    16  )
    17  
    18  func View(res string) {
    19  	resPath, resType, sheet := fileUtils.GetResProp(res, vari.WorkDir)
    20  
    21  	if resType == "yaml" {
    22  		typ, inst, ranges := ReadYamlData(resPath)
    23  		if typ == "inst" && inst.Instances != nil {
    24  			printInst(inst)
    25  		} else if typ == "range" && ranges.Ranges != nil {
    26  			printRanges(ranges)
    27  		} else { // common yaml file, print tips
    28  			logUtils.PrintTo(i118Utils.I118Prt.Sprintf("print_common_yaml_file", res))
    29  		}
    30  	} else if resType == "excel" {
    31  		printExcelSheet(resPath, sheet)
    32  	}
    33  }
    34  
    35  func ReadYamlData(path string) (typ string, insts domain.ResInstances, ranges domain.ResRanges) {
    36  	yamlContent, err := os.ReadFile(path)
    37  	if err != nil {
    38  		logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_read_file", path))
    39  		return
    40  	}
    41  
    42  	err = yaml.Unmarshal(yamlContent, &insts)
    43  	if err == nil && insts.Instances != nil && len(insts.Instances) > 0 {
    44  		typ = "inst"
    45  	} else {
    46  		err = yaml.Unmarshal(yamlContent, &ranges)
    47  		typ = "range"
    48  	}
    49  
    50  	return
    51  }
    52  
    53  func printInst(inst domain.ResInstances) {
    54  	msg := ""
    55  	msg = msg + inst.Title + " " + inst.Desc + "\n"
    56  
    57  	width := 0
    58  	for _, item := range inst.Instances {
    59  		lent := runewidth.StringWidth(item.Instance)
    60  		if lent > width {
    61  			width = lent
    62  		}
    63  	}
    64  
    65  	for idx, item := range inst.Instances {
    66  		if idx > 0 {
    67  			msg = msg + "\n"
    68  		}
    69  		msg = msg + fmt.Sprintf("%d. %s - %s",
    70  			idx+1, item.Instance+strings.Repeat(" ", width-runewidth.StringWidth(item.Instance)), item.Note)
    71  	}
    72  
    73  	logUtils.PrintTo(msg)
    74  }
    75  
    76  func printRanges(ranges domain.ResRanges) {
    77  	msg := ""
    78  	msg = msg + ranges.Title + " " + ranges.Desc + "\n"
    79  
    80  	width := 0
    81  	for name := range ranges.Ranges {
    82  		lent := runewidth.StringWidth(name)
    83  		if lent > width {
    84  			width = lent
    85  		}
    86  	}
    87  
    88  	i := 0
    89  	for name, item := range ranges.Ranges {
    90  		if i > 0 {
    91  			msg = msg + "\n"
    92  		}
    93  		msg = msg + fmt.Sprintf("%d. %s - %s", i+1, name+strings.Repeat(" ", width-runewidth.StringWidth(name)), item)
    94  
    95  		i++
    96  	}
    97  
    98  	logUtils.PrintTo(msg)
    99  }
   100  
   101  func printExcelSheet(path, sheetName string) {
   102  	excel, err := excelize.OpenFile(path)
   103  	if err != nil {
   104  		logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_read_file", path))
   105  		return
   106  	}
   107  
   108  	msg := i118Utils.I118Prt.Sprintf("excel_data_1") + "\n"
   109  
   110  	if sheetName == "" {
   111  		for index, sheet := range excel.GetSheetList() {
   112  			msg = msg + fmt.Sprintf("%d. %s", index+1, sheet) + "\n"
   113  		}
   114  	}
   115  
   116  	logUtils.PrintTo(msg)
   117  
   118  	if sheetName != "" {
   119  		for _, sheet := range excel.GetSheetList() {
   120  			if sheet != sheetName {
   121  				continue
   122  			}
   123  
   124  			widthArr := make([]int, 0)
   125  			dataArr := make([][]string, 0)
   126  			dataArr = append(dataArr, make([]string, 0))
   127  			rows, _ := excel.GetRows(sheet)
   128  
   129  			colCount := 0
   130  			for index, row := range rows {
   131  				if index >= 10 {
   132  					break
   133  				}
   134  
   135  				if index == 0 { // deal with the title
   136  					for _, col := range rows[index] {
   137  						val := strings.TrimSpace(col)
   138  						if val == "" {
   139  							break
   140  						}
   141  
   142  						widthArr = append(widthArr, runewidth.StringWidth(val))
   143  
   144  						dataArr[0] = append(dataArr[0], val)
   145  						colCount++
   146  					}
   147  				} else {
   148  					colArr := make([]string, 0)
   149  					for idx, col := range row {
   150  						if idx >= colCount {
   151  							break
   152  						}
   153  
   154  						val := strings.TrimSpace(col)
   155  
   156  						lent := runewidth.StringWidth(val)
   157  						if widthArr[idx] < lent {
   158  							widthArr[idx] = lent
   159  						}
   160  
   161  						colArr = append(colArr, val)
   162  
   163  					}
   164  					dataArr = append(dataArr, colArr)
   165  				}
   166  			}
   167  
   168  			for _, row := range dataArr {
   169  				line := ""
   170  				for colIdx, col := range row {
   171  					if colIdx >= colCount {
   172  						break
   173  					}
   174  					space := widthArr[colIdx] - runewidth.StringWidth(col)
   175  					line = line + col + strings.Repeat(" ", space) + " "
   176  				}
   177  
   178  				logUtils.PrintTo(line)
   179  			}
   180  		}
   181  	}
   182  }