Refactor a bunch of file stuff to be shorter and cleaner.

This commit is contained in:
David Given
2022-08-27 13:21:03 +02:00
parent cc4bb3a5ec
commit 0d502933ae
7 changed files with 83 additions and 76 deletions

View File

@@ -36,6 +36,12 @@ FlagGroup::FlagGroup()
currentFlagGroup = this;
}
FlagGroup::FlagGroup(std::initializer_list<FlagGroup*> groups):
_groups(groups)
{
currentFlagGroup = this;
}
void FlagGroup::addFlag(Flag* flag)
{
_flags.push_back(flag);

View File

@@ -7,10 +7,9 @@ class ConfigProto;
class FlagGroup
{
private:
FlagGroup(const FlagGroup& group);
public:
FlagGroup();
FlagGroup(std::initializer_list<FlagGroup*> groups);
public:
void parseFlags(int argc, const char* argv[],

View File

@@ -14,6 +14,7 @@ FLUXENGINE_SRCS = \
src/fe-testbandwidth.cc \
src/fe-testvoltages.cc \
src/fe-write.cc \
src/fileutils.cc \
src/fluxengine.cc \
FLUXENGINE_OBJS = $(patsubst %.cc, $(OBJDIR)/%.o, $(FLUXENGINE_SRCS))

View File

@@ -6,40 +6,19 @@
#include "readerwriter.h"
#include "imagereader/imagereader.h"
#include "imagewriter/imagewriter.h"
#include "lib/fluxsource/fluxsource.h"
#include "lib/decoders/decoders.h"
#include "fmt/format.h"
#include "fluxengine.h"
#include "lib/vfs/sectorinterface.h"
#include "lib/vfs/vfs.h"
#include "src/fileutils.h"
#include <google/protobuf/text_format.h>
#include <fstream>
static FlagGroup flags;
static FlagGroup flags({ &fileFlags });
static StringFlag image({"-i", "--image"},
"image to work on",
"",
[](const auto& value)
{
ImageReader::updateConfigForFilename(
config.mutable_image_reader(), value);
});
static StringFlag directory({"-p", "--path"}, "path to list", "");
static char fileTypeChar(FileType file_type)
{
switch (file_type)
{
case TYPE_FILE:
return ' ';
case TYPE_DIRECTORY:
return 'D';
default:
return '?';
}
}
static StringFlag directory({"-p", "--path"}, "path to work on", "");
int mainGetFileInfo(int argc, const char* argv[])
{
@@ -47,16 +26,8 @@ int mainGetFileInfo(int argc, const char* argv[])
showProfiles("ls", formats);
flags.parseFlagsWithConfigFiles(argc, argv, formats);
std::shared_ptr<SectorInterface> sectorInterface;
auto reader = ImageReader::create(config.image_reader());
std::shared_ptr<Image> image(std::move(reader->readImage()));
sectorInterface = SectorInterface::createImageSectorInterface(image);
auto filesystem =
Filesystem::createFilesystem(config.filesystem(), sectorInterface);
Path path(directory);
auto attributes = filesystem->getMetadata(path);
auto filesystem = createFilesystemFromConfig();
auto attributes = filesystem->getMetadata(Path(directory));
for (const auto& e : attributes)
fmt::print("{} = {}\n", e.first, e.second);

View File

@@ -11,28 +11,11 @@
#include "fluxengine.h"
#include "lib/vfs/sectorinterface.h"
#include "lib/vfs/vfs.h"
#include "src/fileutils.h"
#include <google/protobuf/text_format.h>
#include <fstream>
static FlagGroup flags;
static StringFlag image({"-i", "--image"},
"image to work on",
"",
[](const auto& value)
{
ImageReader::updateConfigForFilename(
config.mutable_image_reader(), value);
});
static StringFlag flux({"-f", "--flux"},
"flux source to work on",
"",
[](const auto& value)
{
FluxSource::updateConfigForFilename(
config.mutable_flux_source(), value);
});
static FlagGroup flags({ &fileFlags });
static StringFlag directory({"-p", "--path"}, "path to list", "");
@@ -57,25 +40,8 @@ int mainLs(int argc, const char* argv[])
showProfiles("ls", formats);
flags.parseFlagsWithConfigFiles(argc, argv, formats);
std::shared_ptr<SectorInterface> sectorInterface;
if (config.has_flux_source())
{
std::shared_ptr<FluxSource> fluxSource(FluxSource::create(config.flux_source()));
std::shared_ptr<AbstractDecoder> decoder(AbstractDecoder::create(config.decoder()));
sectorInterface = SectorInterface::createFluxSectorInterface(fluxSource, decoder);
}
else
{
auto reader = ImageReader::create(config.image_reader());
std::shared_ptr<Image> image(std::move(reader->readImage()));
sectorInterface = SectorInterface::createImageSectorInterface(image);
}
auto filesystem =
Filesystem::createFilesystem(config.filesystem(), sectorInterface);
Path path(directory);
auto files = filesystem->list(path);
auto filesystem = createFilesystemFromConfig();
auto files = filesystem->list(Path(directory));
int maxlen = 0;
for (const auto& dirent : files)

55
src/fileutils.cc Normal file
View File

@@ -0,0 +1,55 @@
#include "globals.h"
#include "flags.h"
#include "fluxmap.h"
#include "sector.h"
#include "proto.h"
#include "readerwriter.h"
#include "lib/decoders/decoders.h"
#include "lib/fluxsource/fluxsource.h"
#include "lib/imagereader/imagereader.h"
#include "fmt/format.h"
#include "fluxengine.h"
#include "lib/vfs/sectorinterface.h"
#include "lib/vfs/vfs.h"
#include <google/protobuf/text_format.h>
#include <fstream>
FlagGroup fileFlags;
static StringFlag image({"-i", "--image"},
"image to work on",
"",
[](const auto& value)
{
ImageReader::updateConfigForFilename(
config.mutable_image_reader(), value);
});
static StringFlag flux({"-f", "--flux"},
"flux source to work on",
"",
[](const auto& value)
{
FluxSource::updateConfigForFilename(
config.mutable_flux_source(), value);
});
std::unique_ptr<Filesystem> createFilesystemFromConfig()
{
std::shared_ptr<SectorInterface> sectorInterface;
if (config.has_flux_source())
{
std::shared_ptr<FluxSource> fluxSource(FluxSource::create(config.flux_source()));
std::shared_ptr<AbstractDecoder> decoder(AbstractDecoder::create(config.decoder()));
sectorInterface = SectorInterface::createFluxSectorInterface(fluxSource, decoder);
}
else
{
auto reader = ImageReader::create(config.image_reader());
std::shared_ptr<Image> image(std::move(reader->readImage()));
sectorInterface = SectorInterface::createImageSectorInterface(image);
}
return Filesystem::createFilesystem(config.filesystem(), sectorInterface);
}

9
src/fileutils.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef FILEUTILS_H
#define FILEUTILS_H
extern FlagGroup fileFlags;
extern std::unique_ptr<Filesystem> createFilesystemFromConfig();
#endif