• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lduchosal / ipnetwork / 960

15 May 2026 07:18AM UTC coverage: 0.0% (-94.1%) from 94.053%
960

Pull #411

appveyor

web-flow
Merge ef6b054ce into 4cec191ae
Pull Request #411: Bump MSTest.TestFramework from 4.2.2 to 4.2.3

0 of 2438 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/ConsoleApplication/ActionComputer.cs
1
// <copyright file="ActionComputer.cs" company="IPNetwork">
2
// Copyright (c) IPNetwork. All rights reserved.
3
// </copyright>
4

5
namespace System.Net;
6

7
using System.Collections.Generic;
8
using System.Linq;
9

10
/// <summary>
11
/// Pure computation logic for all CLI actions. Single dispatch point.
12
/// </summary>
13
public static class ActionComputer
14
{
15
    private const string Synopsis =
16
        "ipnetwork [-inmcbflu] [-j] [-d cidr|-D] [-h|-s cidr|-S network|-w|-W|-x|-C network|-o network] networks ...";
17

18
    private static readonly List<string> NetworksExamples =
×
19
        ["1.2.3.4", "10.0.0.0/8", "10.0.0.0/255.0.0.0", "2001:db8::/32", "2001:db8:1:2:3:4:5:6/128"];
×
20

21
    /// <summary>
22
    /// Compute the result for the given action. This is the only switch on Action in the codebase.
23
    /// </summary>
24
    public static ActionOutput Compute(ProgramContext ac, ArgParsed[] argsList)
25
    {
×
26
        return ac.Action switch
×
27
        {
×
28
            Action.Usage => BuildUsageInfo(ac, argsList),
×
29
            Action.PrintNetworks => new ActionOutput.Networks { Items = [.. ac.Networks] },
×
30
            Action.Subnet => ComputeSubnet(ac),
×
31
            Action.Supernet => ComputeSupernet(ac),
×
32
            Action.WideSupernet => ComputeWideSupernet(ac),
×
33
            Action.ContainNetwork => ComputeContain(ac),
×
34
            Action.OverlapNetwork => ComputeOverlap(ac),
×
35
            Action.SubtractNetwork => ComputeSubtract(ac),
×
36
            Action.ListIPAddress => new ActionOutput.IpAddresses
×
37
            {
×
38
                Items = EnumerateIPs(ac),
×
39
                InputNetworks = ac.Networks
×
40
            },
×
41
            _ => new ActionOutput.Error { Message = "Unknown action" }
×
42
        };
×
43
    }
×
44

45
    private static ActionOutput.NetworkGroups ComputeSubnet(ProgramContext ac)
46
    {
×
47
        var groups = new List<List<IPNetwork2>>();
×
48
        foreach (var ipnetwork in ac.Networks)
×
49
        {
×
50
            var group = new List<IPNetwork2>();
×
51
            if (ipnetwork.TrySubnet(ac.SubnetCidr, out IPNetworkCollection? subnets) && subnets is not null)
×
52
            {
×
53
                foreach (var s in subnets)
×
54
                {
×
55
                    group.Add(s);
×
56
                }
×
57
            }
×
58

59
            groups.Add(group);
×
60
        }
×
61

62
        return new ActionOutput.NetworkGroups
×
63
        {
×
64
            Groups = groups,
×
65
            InputNetworks = ac.Networks,
×
66
            SubnetCidr = ac.SubnetCidr
×
67
        };
×
68
    }
×
69

70
    private static ActionOutput ComputeSupernet(ProgramContext ac)
71
    {
×
72
        if (!IPNetwork2.TrySupernet(ac.Networks, out IPNetwork2[]? result))
×
73
        {
×
74
            return new ActionOutput.Error { Message = "Unable to supernet these networks" };
×
75
        }
76

77
        return new ActionOutput.Networks { Items = [.. result] };
×
78
    }
×
79

80
    private static ActionOutput ComputeWideSupernet(ProgramContext ac)
81
    {
×
82
        if (!IPNetwork2.TryWideSubnet(ac.Networks, out IPNetwork2? result))
×
83
        {
×
84
            return new ActionOutput.Error { Message = "Unable to wide subnet these networks" };
×
85
        }
86

87
        return new ActionOutput.Networks { Items = [result] };
×
88
    }
×
89

90
    private static ActionOutput.ContainResults ComputeContain(ProgramContext ac)
91
    {
×
92
        if (ac.ContainNetwork is null)
×
93
        {
×
94
            return new ActionOutput.ContainResults { Items = [] };
×
95
        }
96

97
        var results = new List<ContainInfo>();
×
98
        foreach (var ipnetwork in ac.Networks)
×
99
        {
×
100
            results.Add(new ContainInfo
×
101
            {
×
102
                Network = ac.ContainNetwork.ToString(),
×
103
                Test = ipnetwork.ToString(),
×
104
                Contains = ac.ContainNetwork.Contains(ipnetwork)
×
105
            });
×
106
        }
×
107

108
        return new ActionOutput.ContainResults { Items = results };
×
109
    }
×
110

111
    private static ActionOutput.OverlapResults ComputeOverlap(ProgramContext ac)
112
    {
×
113
        if (ac.OverlapNetwork is null)
×
114
        {
×
115
            return new ActionOutput.OverlapResults { Items = [] };
×
116
        }
117

118
        var results = new List<OverlapInfo>();
×
119
        foreach (var ipnetwork in ac.Networks)
×
120
        {
×
121
            results.Add(new OverlapInfo
×
122
            {
×
123
                Network = ac.OverlapNetwork.ToString(),
×
124
                Test = ipnetwork.ToString(),
×
125
                Overlaps = ac.OverlapNetwork.Overlap(ipnetwork)
×
126
            });
×
127
        }
×
128

129
        return new ActionOutput.OverlapResults { Items = results };
×
130
    }
×
131

132
    private static ActionOutput.SubtractResults ComputeSubtract(ProgramContext ac)
133
    {
×
134
        if (ac.SubtractNetwork is null)
×
135
        {
×
136
            return new ActionOutput.SubtractResults { Items = [] };
×
137
        }
138

139
        var results = new List<IPNetwork2>();
×
140
        foreach (var ipnetwork in ac.Networks)
×
141
        {
×
142
            foreach (var subtracted in ipnetwork.Subtract(ac.SubtractNetwork))
×
143
            {
×
144
                results.Add(subtracted);
×
145
            }
×
146
        }
×
147

148
        return new ActionOutput.SubtractResults { Items = results };
×
149
    }
×
150

151
    private static IEnumerable<string> EnumerateIPs(ProgramContext ac)
152
    {
×
153
        foreach (var ipnetwork in ac.Networks)
×
154
        {
×
155
            foreach (var ip in ipnetwork.ListIPAddress())
×
156
            {
×
157
                yield return ip.ToString();
×
158
            }
×
159
        }
×
160
    }
×
161

162
    private static ActionOutput.UsageInfo BuildUsageInfo(ProgramContext ac, ArgParsed[] argsList)
163
    {
×
164
        var groupOrder = new List<string>();
×
165
        var groups = new Dictionary<string, List<UsageOption>>();
×
166

167
        foreach (var arg in argsList)
×
168
        {
×
169
            if (arg.Group is null || arg.Description is null)
×
170
            {
×
171
                continue;
×
172
            }
173

174
            if (!groups.TryGetValue(arg.Group, out var options))
×
175
            {
×
176
                options = [];
×
177
                groups[arg.Group] = options;
×
178
                groupOrder.Add(arg.Group);
×
179
            }
×
180

181
            options.Add(new UsageOption
×
182
            {
×
183
                Flag = ((char)arg.Arg).ToString(),
×
184
                ArgName = arg.ArgName,
×
185
                Description = arg.Description,
×
186
                Example = arg.Example
×
187
            });
×
188
        }
×
189

190
        string version = typeof(Program).Assembly.GetName().Version?.ToString() ?? "unknown";
×
191

192
        return new ActionOutput.UsageInfo
×
193
        {
×
194
            Errors = ac.ParseErrors,
×
195
            Version = version,
×
196
            Synopsis = Synopsis,
×
197
            OptionGroups = groupOrder.Select(name => new UsageOptionGroup
×
198
            {
×
199
                Name = name,
×
200
                Options = groups[name]
×
201
            }).ToList(),
×
202
            NetworksDescription = "one or more network addresses",
×
203
            NetworksExamples = NetworksExamples
×
204
        };
×
205
    }
×
206
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc