Esempio n. 1
0
// renderTemplate read template and render to file
func renderTemplate(doc *Document) {
	projectDir := env.Get().GetS("GOPATH")
	templateFile := path.Join(
		projectDir, "src/github.com/essentialkaos/shdoc/templates",
		arg.GetS(ARG_TEMPLATE)+".tpl",
	)

	if !fsutil.CheckPerms("FRS", templateFile) {
		printError("Can't read template %s - file is not exist or empty", templateFile)
		os.Exit(1)
	}

	outputFile := arg.GetS(ARG_OUTPUT)

	if fsutil.IsExist(outputFile) {
		os.Remove(outputFile)
	}

	fd, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY, 0644)

	if err != nil {
		printError(err.Error())
		os.Exit(1)
	}

	defer fd.Close()

	tpl, err := ioutil.ReadFile(templateFile)

	if err != nil {
		printError(err.Error())
		os.Exit(1)
	}

	t := template.New("Template")
	t, err = t.Parse(string(tpl[:]))

	err = t.Execute(fd, doc)

	if err != nil {
		printError(err.Error())
		os.Exit(1)
	}

	fmtutil.Separator(false, doc.Title)

	fmtc.Printf("  {*}Constants:{!} %d\n", len(doc.Constants))
	fmtc.Printf("  {*}Variables:{!} %d\n", len(doc.Variables))
	fmtc.Printf("  {*}Methods:{!}   %d\n", len(doc.Methods))
	fmtc.NewLine()
	fmtc.Printf(
		"  {*}Output:{!} %s {s-}(%s){!}\n", outputFile,
		fmtutil.PrettySize(fsutil.GetSize(outputFile)),
	)

	fmtutil.Separator(false)
}
Esempio n. 2
0
// process starting request processing
func process(args []string) {
	var (
		ok    bool
		err   error
		hosts []string
	)

	api, err = sslscan.NewAPI("SSLCli", VER)

	if err != nil {
		if !arg.GetB(ARG_FORMAT) {
			fmtc.Printf("{r}%v{!}\n", err)
		}

		os.Exit(1)
	}

	// By default all fine
	ok = true
	hosts = args

	if fsutil.CheckPerms("FR", hosts[0]) {
		hosts, err = readHostList(hosts[0])

		if err != nil && arg.GetB(ARG_FORMAT) {
			fmtc.Printf("{r}%v{!}\n", err)
			os.Exit(1)
		}
	}

	var (
		grade      string
		checksInfo []*HostCheckInfo
		checkInfo  *HostCheckInfo
	)

	for _, host := range hosts {

		switch {
		case arg.GetB(ARG_QUIET):
			grade, _ = quietCheck(host)
		case arg.GetB(ARG_FORMAT):
			grade, checkInfo = quietCheck(host)
			checksInfo = append(checksInfo, checkInfo)
		default:
			grade = check(host)
		}

		switch {
		case arg.GetB(ARG_PERFECT) && grade != "A+":
			ok = false
		case grade[:1] != "A":
			ok = false
		}
	}

	if arg.GetB(ARG_FORMAT) {
		switch arg.GetS(ARG_FORMAT) {
		case FORMAT_TEXT:
			encodeAsText(checksInfo)
		case FORMAT_JSON:
			encodeAsJSON(checksInfo)
		case FORMAT_XML:
			encodeAsXML(checksInfo)
		case FORMAT_YAML:
			encodeAsYAML(checksInfo)
		default:
			os.Exit(1)
		}
	}

	if arg.GetB(ARG_NOTIFY) {
		fmtc.Bell()
	}

	if !ok {
		os.Exit(1)
	}
}
Esempio n. 3
0
// Parse method parse given file and return document struct and slice with errors
func Parse(file string) (*Document, []error) {
	if !fsutil.CheckPerms("FR", file) {
		return nil, []error{fmt.Errorf("File %s is not readable or not exist", file)}
	}

	fd, err := os.Open(file)

	if err != nil {
		return nil, []error{err}
	}

	defer fd.Close()

	reader := bufio.NewReader(fd)
	scanner := bufio.NewScanner(reader)

	var buffer []string
	var doc = &Document{Title: filepath.Base(file)}

	var lineNum = 0

	for scanner.Scan() {
		line := scanner.Text()
		line = strings.TrimLeft(line, " ")

		lineNum++

		if lineNum == 1 {
			continue
		}

		if line == "" {
			if buffer != nil && !doc.IsValid() {
				doc.About = getCleanData(buffer)
			}

			buffer = nil
			continue
		}

		if strings.Trim(line, "#") == "" {
			if buffer != nil {
				buffer = append(buffer, "")
			}

			continue
		}

		if line[0] == '#' {
			buffer = append(buffer, line[2:])
			continue
		}

		t, name, value := parseEntity(line)

		if t == ENT_TYPE_UNKNOWN || len(buffer) == 0 {
			buffer = nil
			continue
		}

		switch t {
		case ENT_TYPE_METHOD:
			m := parseMethodComment(name, buffer)

			if m == nil {
				buffer = nil
				continue
			}

			m.Line = lineNum

			// Methods MUST have description
			if len(m.Desc) != 0 {
				doc.Methods = append(doc.Methods, m)
			}

			buffer = nil

		case ENT_TYPE_VARIABLE, ENT_TYPE_CONSTANT:
			v := parseVariableComment(name, value, buffer)

			if v == nil {
				buffer = nil
				continue
			}

			// Append multiline parts to value
			if isMultilineValue(value) {
			MULTIPART:
				for scanner.Scan() {
					valuePart := scanner.Text()

					v.Value += valuePart

					if strutil.Tail(valuePart, 1) == "\"" {
						break MULTIPART
					}
				}
			}

			v.Line = lineNum

			// Variables MUST have description
			if len(v.Desc) != 0 {
				if t == ENT_TYPE_VARIABLE {
					doc.Variables = append(doc.Variables, v)
				} else {
					doc.Constants = append(doc.Constants, v)
				}
			}

			buffer = nil
		}
	}

	return doc, []error{}
}