Commit Graph

64 Commits

Author SHA1 Message Date
Thomas Haller
ba350a3495 cli: add additional user-data argument to get_fcn()
The function nmc_print() receives a list of "targets". These are essentially
the rows that should be printed (while the "fields" list represents the columns).

When filling the cells with values, it calles repeatedly get_fcn() on the
column descriptors (fields), by passing each row (target).

The caller must be well aware that the fields and targets are
compatible. For example, in some cases the targets are NMDevice
instances and the target type must correspond to what get_fcn()
expects.

Add another user-data pointer that is passed on along with the
targets. That is useful, if we have a list of targets/rows, but
pass in additional data that applies to all rows alike.

It is still unused.
2018-07-09 15:43:55 +02:00
Thomas Haller
918be83493 cli: rework printing of "wired" and "wifi" device details 2018-07-09 15:43:55 +02:00
Thomas Haller
d6949a2924 cli: rework printing of "capabilities" device details 2018-07-09 15:43:55 +02:00
Thomas Haller
2fc475ccdf cli: rework printing of "connections" device details 2018-07-09 15:43:55 +02:00
Thomas Haller
fede017001 cli: rework printing of "general" device details 2018-07-09 15:43:55 +02:00
Thomas Haller
283b1d18b7 cli: rework printing of device status 2018-07-09 15:43:55 +02:00
Thomas Haller
096ac93f8a cli: rework printing of dhcp options 2018-07-09 15:43:55 +02:00
Thomas Haller
59ea03cc08 cli: rework printing of vpn active-connection properties
use nmc_print() for the job.
2018-07-09 15:43:55 +02:00
Lubomir Rintel
386129d92b cli: add connection FILENAME field
This allows the user to look up the file where the connection is stored.
Useful especially for ifcfg users.

Not shown by default.
2018-06-11 15:09:30 +02:00
Thomas Haller
68fa68b3ed cli: rework printing of general active-connection properties
use nmc_print() for the job.

Also, localize non-terse output.

Also, fix bug with

  $ nmcli c s /org/freedesktop/NetworkManager/ActiveConnection/1

if active connection #1 is invisible to the user.

Also, previously, fill_output_active_connection() wrongly tries to
write to a field that doesn't exist:

  set_val_strc (arr, 13-idx_start, s_con ? nm_setting_connection_get_slave_type (s_con) : NULL);
2018-06-01 16:03:23 +02:00
Thomas Haller
a1b25a47b0 cli: rework printing of nmcli connection for multiple active connections
The output of `nmcli connection show` contains also information about
whether the profile is currently active, for example the device and
the current (activation) state.

Even when a profile can be activated only once (without supporting
mutiple activations at the same time), there are moments when a
connection is activating and still deactivating on another device.
NetworkManager ensures in the case with single activations that
a profile cannot be in state "activated" multiple times. But that
doesn't mean, that one profile cannot have multiple active connection
which reference it. That was already handled wrongly before, because
`nmcli connection show` would only search the first matching
active-connection. That is, it would arbitrarily pick an active
connection in case there were multiple and only show activation
state about one.
Furthermore, we will soon also add the possibility, that a profile can be
active multiple times (at the same time). Especially then, we need to
extend the output format to show all the devices on which the profile is
currently active.

Rework printing the connection list to use nmc_print(), and fix various
issues.

- as discussed, a profile may have multiple active connections at each time.
  There are only two possibilities: if a profile is active multiple
  times, show a line for each activation, or otherwise, show the
  information about multiple activations combined in one line, e.g. by
  printing "DEVICE eth0,eth1". This patch, does the former.
  We will now print a line for each active connection, to show
  all the devices and activation states in multiple lines.
  Yes, this may result in the same profile being printed multiple times.
  That is a change in behavior, and inconvenient if you do something
  like

     for UUID in $(nmcli connection show | awk '{print$2}'); do ...

  However, above is anyway wrong because it assumes that there are no
  spaces in the connection name. The proper way to do this is like

     for UUID in $(nmcli -g UUID connection show); do ...

  In the latter case, whenever a user selects a subset of fields
  (--fields, --get) which don't print information about active connections,
  these multiple lines are combined. So, above still works as expected,
  never returning duplicate UUIDs.

- if a user has no permissions to see a connection, we previously
  would print "<invisible> $NAME". No longer do this but just print
  the ID was it is reported by the active-connection. If the goal
  of this was to prevent users from accidentally access the non-existing
  connection by $NAME, then this was a bad solution, because a script
  would instead try to access "<invisible> $NAME". This is now solved
  better by hiding the active connection if the user selects "-g NAME".

- the --order option now sorts according to how the fields are shown.
  For example, with --terse mode, it will evaluate type "802-11-wireless"
  but with pretty mode it will consider "wifi". This may change the
  ordering in which connections are shown. Also, for sorting the name,
  we use g_utf8_collate() because it's unicode.
2018-06-01 16:03:23 +02:00
Thomas Haller
fc475f25f2 cli: use macro for list of argument for NmcMetaGenericInfo.get_fcn()
The reasons are:

- I want to locate all implmenetations of the get_fcn() handler. By
  consistently using this macro, you can just grep for the macro and
  find them all.

- all implementations should follow the same style. This macro
  enforces the same names for arguments and avoids copy&paste.

- if we are going to add or change an argument, it becomes easier.
  That's because we can easily identify all implementation and can
  change arguments in one place.
2018-05-14 17:53:03 +02:00
Thomas Haller
5975e4a39e cli: add helper methods for implementing NmcMetaGenericInfo's get_fcn() 2018-05-14 17:53:03 +02:00
Thomas Haller
69d5fc9bce cli: add and use macro for creating NmcMetaGenericInfo parent groups 2018-05-14 17:53:03 +02:00
Lubomir Rintel
31aa2cfe29 cli: use a palette to implement coloring
This basically replaces the (NMMetaTermColor, NMMetaTermFormat) combo
with NMMetaColor that describes the colored element semantically as
opposed to storing the raw attributes.

A (currently static) paletted is used to translate the semantic color
code to the actual ANSI controle sequence. This matches what
terminal-colors.d(5) schemes use, making it convenient to implement
customizable palettes.
2018-05-10 14:36:58 +02:00
Lubomir Rintel
9dfe825840 cli: rework enabling and disabling colors
This actually makes very little difference at the moment, but will make
things more confortable later on, when the logic of enabling/disabling
coloring will involve terminal-colors.d(5).

Instead of deciding whether to use colors lazily with use_colors(), it's
done very early on nmcli initialization and a boolean use_colors field
is stored in the NmcConfig instance instead of the raw tristate option
of NmcColorOption type (which is now confined to nmcli.c).

Wherever the NmcColorOption was used previously, the whole NmcConfig
instance is passed around. That might seem pointless (since only the
use_colors boolean is actually used at the moment), but will be utilized
to pass around the actual color palette in future.
2018-05-10 14:36:58 +02:00
Lubomir Rintel
56a5b27389 cli: drop --prompt-color
It's undocumented, useless, somewhat expensive in volume of code and
probably just downright stupid. We'll get a more general way to set
colors.

Hacking in some code to keep this working wouldn't be too difficult, but
it seems entirely pointless.
2018-05-10 14:36:58 +02:00
Beniamino Galvani
1f2319dcb9 clients: change nm_meta_abstract_info_get() to report defaults
Return a boolean to indicate whether the value is the default one, so
that the caller can choose to hide it.
2018-04-13 17:02:55 +02:00
Thomas Haller
5d3736ac65 cli: drop nmc_strsplit_set()
In most cases, it copies the entire strv needlessly.
We can do better.

Also, the max_tokens argument is handled wrongly (albeit
not used anywhere anymore).
2017-12-12 15:19:43 +01:00
Thomas Haller
ccb83e34a3 cli: refactor printing IP6 device info 2017-09-26 19:36:51 +02:00
Thomas Haller
343c967e66 clients: make meta data subtypes of NMObjBaseInst
Yes, this wastes 4 times an unused GType instance
in the class structure.
2017-07-05 14:22:10 +02:00
Lubomir Rintel
24c079e4b2 cli: spawn a pager when running on a terminal
This makes it a lot more convenient to deal with long outputs (such as
"nmcli c show id ...").

The implementation is essentially jacked from systemd. The bugs are
mine.
2017-05-15 17:23:51 +02:00
Thomas Haller
83a6f36207 cli: don't mark field names for translation
Before refactoring nmcli recently, field names were marked for translation.
Note that for the property names, marking them had no effect as only
plain strings can be marked with N_().

Note how --fields are also an input argument. The input should be
independent of the locale and not translated. Likewise, when printing
the header names, they should not be translated to match the --fields
option.

    $ LANG=de_DE.utf8 nmcli --fields GENERAL.DEVICE device show enp0s25
    GENERAL.GERÄT:                          enp0s25

Drop the translation marks.
2017-04-23 23:45:02 +02:00
Thomas Haller
6dfb3e9ffb cli/trivial: remove whitespace between N_ macro and parenthesis
For _() and N_() we don't have a space before the parenthesis.
Be consistent about that.
2017-04-19 18:27:19 +02:00
Thomas Haller
886dad49d4 cli: move parsing of meta data fields 2017-04-13 10:10:12 +02:00
Thomas Haller
39bc8dbdf9 cli/trivial: rename NmcOutputSelectionItem type 2017-04-13 10:10:12 +02:00
Thomas Haller
b7efa62745 cli: cleanup meta data virtual function arguments
- have the "self" argument first, before the environment arguments.
  It's more idiomatic.
- from within cli, always pass nmc_meta_environment and nmc_meta_arg
  where needed.
- drop the union in NMMetaAbstractInfo. I was suppost to make casts
  nicer, but it doesn't really.
2017-04-12 14:00:00 +02:00
Thomas Haller
41b31051f2 cli: use nmc_print() to output device's IP4 info
The IP4 info adds a new type: to expose strv arguments
for addresses, etc.
2017-04-12 14:00:00 +02:00
Thomas Haller
03307e3dd6 cli: use nmc_print() to output nmcli general logging 2017-04-12 11:24:04 +02:00
Thomas Haller
16299e5ac0 cli: use nmc_print() to output nmcli general permissions
Add also colors for the output values.
2017-04-12 11:24:04 +02:00
Thomas Haller
fdd40b6a68 cli: implement new nmc_print() command to generically output cli data
We already have
  - data sources (nm_cli, connections or settings)
  - meta data information how to access the data sources (NMMetaAbstractInfo,
    NmcMetaGenericInfo, NMMetaPropertyInfo)

Add now a generic way to output cli data using nmc_print(). It gets a
list of data-sources (@targets) and a list of available fields (meta
data). It also gets cli configuration (NmcConfig) and field selector
strings (@field_str).

Based on that, it should output the desired data.

This is intended to replaces the previous approach, where functions like
show_nm_status() have full knowledge about how to access the data and
create an intermediate output format (NmcOutputData, NmcOutputField)
that was printed via print_data().
show_nm_status() contained both knowledge about the data itself (how to
print a value) and intimate knoweledge about the output intermediate
format. Also, the intermediate format is hard to understand. For
example, sometimes we put the field prefix in NmcOutputField at index 0
and via the NmcOfFlags we control how to output the data.

Clearly separate the responsibilities.

  - The meta data (NmcMetaGenericInfo) is only concerned with converting
    a data source to a string (or a color format).
  - the field selection (@field_str) only cares about parsing the list
    of NMMetaAbstractInfo.
  - _print_fill() populates a table with output values and header
    entries.
  - _print_do() prints the previously prepared table.

The advantage is that if you want to change anything, you only need to
touch a particular part.

This is only a show-case for `nmcli general status`. Parts are still
un-implemented and will follow.

This changes behavior for --terse mode: the values are now no longer
translated:
  $ LANG=de_DE.utf8 nmcli -t --mode multiline general
2017-04-12 11:24:04 +02:00
Thomas Haller
ca0e749c40 cli: move and rename TermColor and TermFormat 2017-04-05 16:53:06 +02:00
Thomas Haller
e3a07633dc cli: fix signature of NMMetaAbstractType:get_fcn()
Depending on the get_type argument, we don't only want
to return strings, but arbitrary pointers.

The out_to_free argument still makes sense, but depending on
the get-type you must know how to free the pointer.
2017-04-05 16:53:06 +02:00
Thomas Haller
9276655975 cli: move NmcMetaGenericInfo to "utils.h" 2017-04-05 16:53:06 +02:00
Thomas Haller
137273669d cli: add nmc_output_selection_create() to parse field selection
nmc_output_selection_create() returns a less opaque result then
a GArray and a GPtrArray for the groups.
2017-04-05 16:53:06 +02:00
Thomas Haller
bfb9fd0d2f cli: split tracking of meta data out of NmcOutputField
When generating output data, nmcli iterates over a list of
property-descriptors (nmc_fields_ip4_config), creates an intermediate
array (output_data) and finally prints it.

However, previously both the meta data (nmc_fields_ip4_config) and
the intermediate format use the same type NmcOutputField. This means,
certain fields are relevant to describe a property, and other fields
are output/formatting fields.

Split this up. Now, the meta data is tracked in form of an NMMetaAbstractInfo
lists. This separates the information about properties from intermediate steps
during creation of the output.

Note that currently functions like print_ip4_config() still have all the
knowledge about how to generate the output. That is wrong, instead, the
meta data (NMMetaAbstractInfo) should describe how to create the output
and then all those functions could be replaced. This means, later we want
to add more knowledge to the NMMetaAbstractInfo, so it is important to
keep them separate from NmcOutputField.
2017-04-05 16:53:06 +02:00
Thomas Haller
aae721d0df cli: pass arguments for print_data separately of NmcOutputData
Don't pass on large structs of input arguments. It only convolutes
which arguments are passed, and where they come from.
2017-04-05 16:53:06 +02:00
Thomas Haller
f973f0841a cli: merge NmcPrintFields into NmcOutputData and pass it directly to print_required_fields() 2017-04-05 16:53:06 +02:00
Thomas Haller
f12106d83a cli: use enum NmcOfFlags instead of plain integer flags and pass to print_required_fields() 2017-04-05 16:53:06 +02:00
Francesco Giudici
6a3d77fbe6 nmcli: allow cmd specific option parsing in next_arg() func
Options dependant on specific commands (e.g., nmcli connection show
--active) are now allowed to be processed by the next_arg() function.
This would allow autocompletion to expand options belonging to specific
command first, and then global ones.

Note that global options ("--ask" and "--show-secrets") will be auto-completed
everywhere but only if at least a '-' is passed. Command specific ones
(--temporary, --active, --order) will be auto-completed only after the command
they belongs to but without requiring the user to pass a heading '-'.

Example:
'nmcli connection show -a'
will expand '-a' into '--active', but
'nmcli connection add -a`
will expand '-a' into '--ask' (as it is a global option)

This commit fixes also autocompletion for:
nmcli connection modify --temporary
2017-04-05 15:43:40 +02:00
Thomas Haller
85acdd70e7 cli: split print_data() in a part with and without side-effects
To better understand which part of the code have side effects,
split print_data() in a part that mutilates the input array
and a part that only prints.
2017-03-30 13:46:09 +02:00
Thomas Haller
2ea670ab96 cli: split output data from NmCli to a separate field 2017-03-30 13:34:35 +02:00
Thomas Haller
98ce4a8d5d cli: separate input and in-out arguments in print_data()
Don't pass down the entire NmCli instance. The output_data
array is modified by print_data(), the rest is read-only input.
2017-03-30 13:09:59 +02:00
Thomas Haller
9359d5622e cli: pass use_colors as separate option instead of global nmc
nmc contains all options and collects output data. It is very hard to
understand what it does. Start splitting it up, and pass arguments along
-- as needed.
2017-03-30 13:09:59 +02:00
Thomas Haller
23298bfc88 cli: move utils function from common.h to nm-meta-setting-desc.c
These functions are only used by nm-meta-setting-desc.c. Make them internal.
Unfortunately, they are part of "common.h" which cannot be used without
the rest of nmcli. Still todo.
2017-03-30 13:09:58 +02:00
Thomas Haller
cb66cf96d7 cli: add values_func() to NmcPropertyInfo and use for "connection" setting 2017-03-30 13:09:55 +02:00
Lubomir Rintel
5a71bc8022 cli: add NmCli argument to next_arg()
Will be useful in next commit.
2017-03-28 15:32:05 +02:00
Francesco Giudici
7d96f1f17d nmcli: don't enforce anymore the -f(ields) option in -t(erse) mode 2017-03-28 10:55:00 +02:00
Lubomir Rintel
84c484ed5b cli: make match() return boolean
Coccinelle semantic patch:

  @@
  @@
  -int
  +gboolean
          matches (...);

  @@
  expression pattern, cmd, len;
  @@
  -int
  +gboolean
          matches (...)
          {
                  ...
  -               return memcmp (pattern, cmd, len);
  +               return memcmp (pattern, cmd, len) == 0;
          }

  @@
  expression prefix, str;
  @@
  (
  -matches (prefix, str) != 0
  +!matches (prefix, str)
  |
  -matches (prefix, str) == 0
  +matches (prefix, str)
  )

  @@
  expression prefix, str;
  @@
  -(matches (prefix, str))
  +matches (prefix, str)

  @@
  expression prefix, str;
  @@
  -(!matches (prefix, str))
  +!matches (prefix, str)

spatch --smpl-spacing --sp-file match.cocci --dir clients/cli/ \
  --include-headers --macro-file shared/nm-utils/gsystem-local-alloc.h
2017-02-15 13:04:07 +01:00
Thomas Haller
348e505b3a macros: add macro _nm_printf() for function attribute 2016-06-05 12:22:01 +02:00