aboutsummaryrefslogtreecommitdiff
path: root/src/zen/zen.h
blob: 2f0a02e6ae2ed650875204463ba54b30852c2b3f (plain) (blame)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include <zencore/except.h>
#include <zencore/timer.h>
#include <zencore/uid.h>
#include <zencore/zencore.h>
#include <zenhttp/httpclient.h>
#include <zenutil/config/commandlineoptions.h>
#include <zenutil/config/loggingconfig.h>

#include <csignal>

namespace zen {

struct ZenCliOptions
{
	bool IsDebug				= false;
	bool IsVerbose				= false;
	bool EnableExecutionHistory = true;

	ZenLoggingConfig LoggingConfig;

	std::string HttpClientBackend;	// Choice of HTTP client implementation
	Oid			ParentSessionId = Oid::Zero;

	// Arguments after " -- " on command line are passed through and not parsed
	std::string				 PassthroughCommandLine;
	std::string				 PassthroughArgs;
	std::vector<std::string> PassthroughArgV;
};

struct ZenCmdCategory
{
	std::string						   Name;
	std::map<std::string, std::string> SortedCmds;
};

extern ZenCmdCategory g_UtilitiesCategory;
extern ZenCmdCategory g_ProjectStoreCategory;
extern ZenCmdCategory g_CacheStoreCategory;
extern ZenCmdCategory g_StorageCategory;

// RAII wrapper around `signal(2)` that restores the previous handler on scope
// exit. Use in command Run() methods so an exception during option parsing or
// execution doesn't leave a handler installed whose flag is scoped to a
// now-dead lifetime.
class ScopedSignalHandler
{
public:
	using Handler = void (*)(int);

	ScopedSignalHandler(int SigNum, Handler NewHandler) : m_SigNum(SigNum), m_PrevHandler(std::signal(SigNum, NewHandler)) {}

	~ScopedSignalHandler()
	{
		if (m_PrevHandler != SIG_ERR)
		{
			std::signal(m_SigNum, m_PrevHandler);
		}
	}

	ScopedSignalHandler(const ScopedSignalHandler&) = delete;
	ScopedSignalHandler& operator=(const ScopedSignalHandler&) = delete;

private:
	int		m_SigNum;
	Handler m_PrevHandler;
};

class ErrorWithReturnCode : public std::runtime_error
{
public:
	using _Mybase = runtime_error;
	ErrorWithReturnCode(const std::string& Message, int InReturnCode) : _Mybase(Message), m_ReturnCode(InReturnCode) {}

	ErrorWithReturnCode(const char* Message, int InReturnCode) : _Mybase(Message), m_ReturnCode(InReturnCode) {}

	const int m_ReturnCode = -1;
};

/** Base class for command implementations
 */

class ZenCmdBase
{
public:
	virtual void			  Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) = 0;
	virtual cxxopts::Options& Options()														 = 0;
	virtual ZenCmdCategory&	  CommandCategory() const;

	// Hidden commands are dispatched normally but omitted from the top-level
	// `zen --help` listing. Used for deprecated aliases that remain functional
	// but should not be advertised.
	virtual bool IsHidden() const { return false; }

	bool			   ParseOptions(int argc, char** argv);
	static bool		   ParseOptions(cxxopts::Options& Options, int argc, char** argv);
	static bool		   ParseOptionsPermissive(cxxopts::Options& Options, int argc, char** argv, std::vector<std::string>& OutUnmatched);
	static int		   GetSubCommand(cxxopts::Options&			  Options,
									 int						  argc,
									 char**						  argv,
									 std::span<cxxopts::Options*> SubOptions,
									 cxxopts::Options*&			  OutSubOption,
									 std::vector<char*>&		  OutSubCommandArguments);
	static std::string ResolveTargetHostSpec(const std::string& InHostSpec);
	static std::string ResolveTargetHostSpec(const std::string& InHostSpec, uint16_t& OutEffectivePort);

	static bool		  IsUnixSocketSpec(std::string_view Spec);
	static HttpClient CreateHttpClient(const std::string& HostSpec, HttpClientSettings Settings = {});

	static constexpr const char* kHostUrlHelp = "Host URL or unix:///path/to/socket";

	std::string		  HelpText();
	[[noreturn]] void ThrowOptionError(std::string_view Message);
	static void		  LogExecutableVersionAndPid();
};

class StorageCommand : public ZenCmdBase
{
	virtual ZenCmdCategory& CommandCategory() const override { return g_StorageCategory; }
};

class CacheStoreCommand : public ZenCmdBase
{
	virtual ZenCmdCategory& CommandCategory() const override { return g_CacheStoreCategory; }
};

// Base for individual subcommands
class ZenSubCmdBase
{
public:
	ZenSubCmdBase(std::string_view Name, std::string_view Description);
	virtual ~ZenSubCmdBase() = default;
	cxxopts::Options&				SubOptions() { return m_SubOptions; }
	std::string_view				Description() const { return m_Description; }
	const std::vector<std::string>& Aliases() const { return m_Aliases; }
	virtual void					Run(const ZenCliOptions& GlobalOptions) = 0;

protected:
	void AddAlias(std::string Alias) { m_Aliases.push_back(std::move(Alias)); }

	cxxopts::Options m_SubOptions;

private:
	std::string				 m_Description;
	std::vector<std::string> m_Aliases;
};

// Base for commands that host subcommands - handles all dispatch boilerplate
class ZenCmdWithSubCommands : public ZenCmdBase
{
public:
	void Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) final;

protected:
	void		 AddSubCommand(ZenSubCmdBase& SubCmd);
	virtual bool OnParentOptionsParsed(const ZenCliOptions& GlobalOptions);
	void		 PrintHelp();
	void		 PrintSubCommandHelp(cxxopts::Options& SubCmdOptions);

private:
	std::vector<ZenSubCmdBase*> m_SubCommands;
};

class CacheStoreCmdWithSubCommands : public ZenCmdWithSubCommands
{
	ZenCmdCategory& CommandCategory() const override { return g_CacheStoreCategory; }
};

class ProjectStoreCmdWithSubCommands : public ZenCmdWithSubCommands
{
	ZenCmdCategory& CommandCategory() const override { return g_ProjectStoreCategory; }
};

class StorageCmdWithSubCommands : public ZenCmdWithSubCommands
{
	ZenCmdCategory& CommandCategory() const override { return g_StorageCategory; }
};

}  // namespace zen