generated from origadmin/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.go
124 lines (105 loc) · 2.63 KB
/
bootstrap.go
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
122
123
124
/*
* Copyright (c) 2024 OrigAdmin. All rights reserved.
*/
// Package bootstrap is a package that provides the bootstrap information for the service.
package bootstrap
import (
"path/filepath"
"time"
)
// Constants for default paths and environment
const (
DefaultConfigPath = "configs/config.toml"
DefaultEnv = "release"
DefaultWorkDir = "."
)
// Bootstrap struct to hold bootstrap information
type Bootstrap struct {
Flags Flags
WorkDir string
ConfigPath string
Env string
Daemon bool
}
var (
buildEnv = DefaultEnv
)
// SetFlags sets the flags for the bootstrap
func (b *Bootstrap) SetFlags(name, version string) {
b.Flags.Version = version
b.Flags.ServiceName = name
}
// ServiceID returns the service ID
func (b *Bootstrap) ServiceID() string {
return b.Flags.ServiceID()
}
// ID returns the ID
func (b *Bootstrap) ID() string {
return b.Flags.ID
}
// Version returns the version
func (b *Bootstrap) Version() string {
return b.Flags.Version
}
// ServiceName returns the service name
func (b *Bootstrap) ServiceName() string {
return b.Flags.ServiceName
}
// StartTime returns the start time
func (b *Bootstrap) StartTime() time.Time {
return b.Flags.StartTime
}
// Metadata returns the metadata
func (b *Bootstrap) Metadata() map[string]string {
return b.Flags.Metadata
}
// WorkPath returns the work path
func (b *Bootstrap) WorkPath() string {
// set workdir to current directory if not set
if b.WorkDir == "" {
b.WorkDir = DefaultWorkDir
}
// if the workdir is not absolute path, make it absolute
b.WorkDir, _ = filepath.Abs(b.WorkDir)
// return the workdir, if the config path is empty
if b.ConfigPath == "" {
return b.WorkDir
}
// if the config path is absolute path, return it
if filepath.IsAbs(b.ConfigPath) {
return b.ConfigPath
}
// point the path to the `workdir path/config path`, and make it absolute
path := filepath.Join(b.WorkDir, b.ConfigPath)
path, _ = filepath.Abs(path)
return path
}
// DefaultBootstrap returns a default bootstrap
func DefaultBootstrap() *Bootstrap {
return &Bootstrap{
WorkDir: DefaultWorkDir,
ConfigPath: DefaultConfigPath,
Env: DefaultEnv,
Daemon: false,
Flags: DefaultFlags(),
}
}
// New returns a new bootstrap
func New(dir, path string) *Bootstrap {
return &Bootstrap{
WorkDir: dir,
ConfigPath: path,
Env: DefaultEnv,
Daemon: false,
Flags: DefaultFlags(),
}
}
func WithFlags(name string, version string) *Bootstrap {
return &Bootstrap{
WorkDir: DefaultWorkDir,
ConfigPath: DefaultConfigPath,
Env: DefaultEnv,
Daemon: false,
Flags: NewFlags(name, version),
}
}