File: docs.go

package info (click to toggle)
singularity-container 4.0.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,672 kB
  • sloc: asm: 3,857; sh: 2,125; ansic: 1,677; awk: 414; makefile: 110; python: 99
file content (85 lines) | stat: -rw-r--r-- 2,627 bytes parent folder | download
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
// Copyright (c) 2019-2020, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.

package main

import (
	"fmt"

	"github.com/spf13/cobra"
	"github.com/spf13/cobra/doc"
	"github.com/sylabs/singularity/v4/cmd/internal/cli"
	"github.com/sylabs/singularity/v4/pkg/sylog"
	"golang.org/x/sys/unix"
)

func assertAccess(dir string) {
	if err := unix.Access(dir, unix.W_OK); err != nil {
		sylog.Fatalf("Given directory (%s) does not exist or is not writable by calling user", dir)
	}
}

func markdownDocs(rootCmd *cobra.Command, outDir string) {
	assertAccess(outDir)
	sylog.Infof("Creating Singularity markdown docs at %s\n", outDir)
	if err := doc.GenMarkdownTree(rootCmd, outDir); err != nil {
		sylog.Fatalf("Failed to create markdown docs for singularity\n")
	}
}

func manDocs(rootCmd *cobra.Command, outDir string) {
	assertAccess(outDir)
	sylog.Infof("Creating Singularity man pages at %s\n", outDir)
	header := &doc.GenManHeader{
		Title:   "singularity",
		Section: "1",
	}

	// works recursively on all sub-commands (thanks bauerm97)
	if err := doc.GenManTree(rootCmd, header, outDir); err != nil {
		sylog.Fatalf("Failed to create man pages for singularity\n")
	}
}

func rstDocs(rootCmd *cobra.Command, outDir string) {
	assertAccess(outDir)
	sylog.Infof("Creating Singularity RST docs at %s\n", outDir)
	if err := doc.GenReSTTreeCustom(rootCmd, outDir, func(a string) string {
		return ""
	}, func(name, ref string) string {
		return fmt.Sprintf(":ref:`%s <%s>`", name, ref)
	}); err != nil {
		sylog.Fatalf("Failed to create RST docs for singularity\n")
	}
}

func main() {
	var dir string
	rootCmd := &cobra.Command{
		ValidArgs: []string{"markdown", "man", "rst"},
		Args:      cobra.ExactArgs(1),
		Use:       "makeDocs {markdown | man | rst}",
		Short:     "Generates Singularity documentation",
		Run: func(cmd *cobra.Command, args []string) {
			// We must Init() as loading commands etc. is deferred until this is called.
			// Using true here will result in local docs including any content for installed
			// plugins.
			cli.Init(true)
			rootCmd := cli.RootCmd()
			switch args[0] {
			case "markdown":
				markdownDocs(rootCmd, dir)
			case "man":
				manDocs(rootCmd, dir)
			case "rst":
				rstDocs(rootCmd, dir)
			default:
				sylog.Fatalf("Invalid output type %s\n", args[0])
			}
		},
	}
	rootCmd.Flags().StringVarP(&dir, "dir", "d", ".", "Directory in which to put the generated documentation")
	rootCmd.Execute()
}