-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
121 lines (111 loc) · 4.88 KB
/
main.go
File metadata and controls
121 lines (111 loc) · 4.88 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2024, TeamDev. All rights reserved.
//
// Redistribution and use in source and/or binary forms, with or without
// modification, must retain the above copyright notice and the following
// disclaimer.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package main
import (
"embed-code/embed-code-go/cli"
"log/slog"
)
// The entry point for embed-code.
//
// There are three modes, which are chosen by 'mode' arg. If it is set to 'check',
// then the checking for up-to-date is performed. If it is set to 'embed', the embedding is
// performed. If it is set to 'analyze', the analyzing is performed.
//
// EmbeddingInstruction is the process that consists of the following steps:
// - the code fragments are extracted from the code files;
// - the docs files are scanned for <embed-code> tags;
// - for each tag, the code fragments are embedded into the docs. The embedding
// is parametrized with the tag attributes.
//
// Checking for up-to-date is the process that consists of the following steps:
// - the code fragments are extracted from the code files;
// - the docs files are scanned for <embed-code> tags;
// - for each tag, the code fragments are compared to the code which is already embedded
// into the docs;
// - if there is a difference, the error is reported.
//
// The 'mode' arg is required.
//
// Embed code also needs root directories to be set.
// There are two options to set them:
// - code-path and docs-path args, in this case roots are read directly from provided paths;
// - config-path arg, in this case roots are read from the given config file.
//
// If both options are missed, the embedding fails.
// If both options are set, the embedding fails as well.
// If config file is not exists or does not contain 'code-path' and 'docs-path' fields, the
// embedding fails.
//
// All possible args:
// - code-path — a path to a root directory with code files;
// - docs-path — a path to a root directory with docs files;
// - config-path — a path to a yaml configuration file;
// - mode — string which represents the mode of embed-code execution. if it is set to 'check',
// then the checking for up-to-date is performed. If it is set to 'embed', the embedding
// is performed.
// If it is set to 'analyze', the analyzing is performed;
// - code-includes — a comma-separated string of glob patterns for code files to include.
// For example:
// "**/*.java,**/*.gradle". Default value is "**/*.*";
// - doc-includes — a comma-separated string of glob patterns for docs files to include.
// For example:
// "docs/**/*.md,guides/*.html". Default value is "**/*.md,**/*.html";
// - doc-excludes - a comma-separated string of glob patterns for docs files to exclude from
// the embedding.
// For example:
// "old-docs/**/*.md,old-guides/*.html". It is not set by default;
// - fragments-path — a path to a directory with code fragments. Default value is
// "./build/fragments";
// - separator — a string which is used as a separator between code fragments. Default value
// is "...".
func main() {
slog.Info("starting application, reading args...")
userArgs := cli.ReadArgs()
if cli.IsUsingConfigFile(userArgs) {
err := cli.ValidateConfigFile(userArgs)
if err != nil {
slog.Error("the provided config file is not valid.", "error", err)
return
}
userArgs, err = cli.FillArgsFromConfigFile(userArgs)
if err != nil {
slog.Error("received an issue while reading config file: ", "error", err)
return
}
}
err := cli.ValidateConfig(userArgs)
if err != nil {
slog.Error("user arguments are not valid.", "error", err)
return
}
configs := cli.BuildEmbedCodeConfiguration(userArgs)
for _, config := range configs {
switch userArgs.Mode {
case cli.ModeCheck:
cli.CheckCodeSamples(config)
slog.Info("the documentation files are up-to-date with code files.")
case cli.ModeEmbed:
cli.EmbedCodeSamples(config)
cli.CheckCodeSamples(config)
slog.Info("the code fragments are successfully embedded.")
case cli.ModeAnalyze:
cli.AnalyzeCodeSamples(config)
slog.Info("analysis is completed, analytics files can be found in /build/analytics folder")
}
}
}