34 lines
790 B
C++
34 lines
790 B
C++
#pragma once
|
|
|
|
#include "cmd/cmd_base.hpp"
|
|
#include <fstream>
|
|
#include <ios>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
namespace fsh {
|
|
|
|
class CmdTruncate : public Command {
|
|
|
|
protected:
|
|
virtual void register_flags() override {
|
|
ArgFactory& factory = get_factory();
|
|
factory.add_rule<std::string>(1);
|
|
}
|
|
|
|
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
|
|
|
|
std::string filename = args.get<std::string>(0).value();
|
|
|
|
if(!std::ifstream(filename, std::ios_base::in)) {
|
|
throw util::FileExistsError();
|
|
}
|
|
|
|
if(!std::ofstream(filename, std::ios_base::out)) {
|
|
throw util::CmdError("Failed to truncate");
|
|
}
|
|
|
|
}
|
|
};
|
|
|
|
} |