aboutsummaryrefslogtreecommitdiff
path: root/bbot/utility.hxx
blob: 7758db440c20f76926fa041dde39209e33c9b0b8 (plain)
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
// file      : bbot/utility.hxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef BBOT_UTILITY_HXX
#define BBOT_UTILITY_HXX

#include <memory>    // make_shared()
#include <string>    // to_string(), stoull()
#include <utility>   // move(), forward(), declval(), make_pair()
#include <cassert>   // assert()
#include <iterator>  // make_move_iterator()
#include <algorithm> // *

#include <libbutl/ft/lang.hxx>

#include <libbutl/curl.hxx>
#include <libbutl/process.hxx>
#include <libbutl/process-io.hxx>
#include <libbutl/utility.hxx>    // icasecmp(), reverse_iterate(), etc
#include <libbutl/filesystem.hxx>

#include <bbot/types.hxx>
#include <bbot/version.hxx>

namespace bbot
{
  using std::move;
  using std::forward;
  using std::declval;

  using std::make_pair;
  using std::make_shared;
  using std::make_move_iterator;
  using std::to_string;
  using std::stoull;

  // <libbutl/utility.hxx>
  //
  using butl::icasecmp;
  using butl::sanitize_identifier;
  using butl::reverse_iterate;

  using butl::make_guard;
  using butl::make_exception_guard;

  using butl::getenv;
  using butl::setenv;
  using butl::unsetenv;

  // <libbutl/filesystem.hxx>
  //
  using butl::auto_rmdir;
  using butl::auto_rmfile;

  // Process execution.
  //
  class tracer;

  using butl::process;
  using butl::process_env;
  using butl::process_exit;
  using butl::process_error;

  template <typename I, typename O, typename E, typename P, typename... A>
  void
  run_io (tracer&, I&& in, O&& out, E&& err, const P&, A&&...);

  template <typename I, typename O, typename E, typename P, typename... A>
  process_exit::code_type
  run_io_exit (tracer&, I&& in, O&& out, E&& err, const P&, A&&...);

  template <typename I, typename O, typename E, typename... A>
  process
  run_io_start (tracer&,
                I&& in,
                O&& out,
                E&& err,
                const process_env&,
                A&&...);

  template <typename P>
  void
  run_io_finish (tracer&, process&, const P&, bool fail_hard = true);

  template <typename P>
  process_exit::code_type
  run_io_finish_exit (tracer&, process&, const P&, bool fail_hard = true);

  template <typename P, typename... A>
  inline void
  run (tracer& t, const P& p, A&&... a)
  {
    run_io (t, butl::fdopen_null (), 2, 2, p, forward<A> (a)...);
  }

  template <typename P, typename... A>
  inline process_exit::code_type
  run_exit (tracer& t, const P& p, A&&... a)
  {
    return run_io_exit (
      t, butl::fdopen_null (), 2, 2, p, forward<A> (a)...);
  }

  // The curl process wrapper (command line tracing, etc).
  //
  class http_curl: public butl::curl
  {
  public:
    template <typename I, typename O, typename... A>
    http_curl (tracer&,
               I&& in,
               O&& out,
               method_type,
               const string& url,
               A&&... options);
  };

  class tftp_curl: public butl::curl
  {
  public:
    template <typename I, typename O, typename... A>
    tftp_curl (tracer&,
               I&& in,
               O&& out,
               method_type,
               const string& url,
               A&&... options);
  };

  // Manifest parsing and serialization.
  //
  // For parsing, if path is '-', then read from stdin. If path has the .lz4
  // extension, then assume the content is compressed.
  //
  template <typename T>
  T
  parse_manifest (const path&,
                  const char* what,
                  bool fail_hard = true,
                  bool ignore_unknown = true);

  template <typename T>
  T
  parse_manifest (istream&,
                  const string& name,
                  const char* what,
                  bool fail_hard = true,
                  bool ignore_unknown = true);

  template <typename T>
  void
  serialize_manifest (const T&,
                      const path&,
                      const char* what,
                      bool long_lines = false);

  template <typename T>
  void
  serialize_manifest (const T&,
                      ostream&,
                      const string& name,
                      const char* what,
                      bool fail_hard = true,
                      bool long_lines = false);
}

#include <bbot/utility.txx>

#endif // BBOT_UTILITY_HXX