aboutsummaryrefslogtreecommitdiff
path: root/src/zen/zen.h
blob: 40c745bc71fb1fea4200ef610f4f2cf4d0b5bb69 (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
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include <zencore/except.h>
#include <zencore/timer.h>
#include <zencore/zencore.h>
#include <zenutil/commandlineoptions.h>

namespace cpr {
class Response;
}

namespace zen {

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

	// 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;

/** Base class for command implementations
 */

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

	bool			   ParseOptions(int argc, char** argv);
	static bool		   ParseOptions(cxxopts::Options& Options, int argc, char** argv);
	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 FormatHttpResponse(const cpr::Response& Response);
	static int		   MapHttpToCommandReturnCode(const cpr::Response& Response);
	static std::string ResolveTargetHostSpec(const std::string& InHostSpec);
	static std::string ResolveTargetHostSpec(const std::string& InHostSpec, uint16_t& OutEffectivePort);
};

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

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

class ProgressBar
{
public:
	struct State
	{
		bool		operator==(const State&) const = default;
		std::string Task;
		std::string Details;
		uint64_t	TotalCount	   = 0;
		uint64_t	RemainingCount = 0;
		enum class EStatus
		{
			Running,
			Aborted,
			Paused
		};
		EStatus Status = EStatus::Running;

		static EStatus CalculateStatus(bool IsAborted, bool IsPaused)
		{
			if (IsAborted)
			{
				return EStatus::Aborted;
			}
			if (IsPaused)
			{
				return EStatus::Paused;
			}
			return EStatus::Running;
		}
	};

	enum class Mode
	{
		Plain,
		Pretty,
		Log,
		Quiet
	};

	static void SetLogOperationName(Mode InMode, std::string_view Name);
	static void SetLogOperationProgress(Mode InMode, uint32_t StepIndex, uint32_t StepCount);

	explicit ProgressBar(Mode InMode, std::string_view InSubTask);
	~ProgressBar();

	void UpdateState(const State& NewState, bool DoLinebreak);
	void ForceLinebreak();
	void Finish();
	bool IsSameTask(std::string_view Task) const;
	bool HasActiveTask() const;

private:
	const Mode		  m_Mode;
	Stopwatch		  m_SW;
	uint64_t		  m_LastUpdateMS;
	uint64_t		  m_PausedMS;
	State			  m_State;
	const std::string m_SubTask;
	size_t			  m_LastOutputLength = 0;
};

}  // namespace zen